Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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

回答(2 个)

Florian Floh
Florian Floh 2020-4-4
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 2020-4-4
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'}
>>

标签

Community Treasure Hunt

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

Start Hunting!

Translated by