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
댓글 수: 0
답변 (2개)
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
댓글 수: 0
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'}
>>
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!