Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Alex Doan
Alex Doan 2020 年 4 月 4 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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