Info

This question is closed. Reopen it to edit or answer.

how to store even and odd strings into 2 seperate vectors using mod function

1 view (last 30 days)
For example Names is a 16x1
1.) a
2.) b
3.)c
4.)d
5.)e
i want to store a,c,e in variable core 1 and i want to store b,d in core 2
This is what i mean by storing the even and odd strings using mod

Answers (2)

Florian Floh
Florian Floh on 4 Apr 2020
This code should do the trick:
names = ['c','a', 'b','z','x','s'];
oddlett = [];
evenlett = [];
[n,m] = size(names);
for i=1:m
% convert letter to corresponding index in the alphabet
ind = 1 + lower(names(i)) - 'a';
if(mod(ind, 2) ==1)
evenlett = [evenlett; names(i)];
else
oddlett = [oddlett; names(i)];
end
end

dpb
dpb on 4 Apr 2020
Whassup w/ this thing about alternative storage of odd/even indices all of a sudden???
<Answers/514742-how-to-separate-an-array-into-two> altho as pointed out there first, you don't need either a loop or the mod function to do it...
>> names=cellstr(['a':'e'].');
>> n1=names(1:2:end)
n1 =
3×1 cell array
{'a'}
{'c'}
{'e'}
>> n2=names(2:2:end)
n2 =
2×1 cell array
{'b'}
{'d'}
>>

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!