How do I replace elements of an array with letters?

조회 수: 1 (최근 30일)
Cai Chin
Cai Chin 2021년 1월 23일
편집: Adam Danz 2021년 1월 26일
Hi, I have an array called 'labels_I03' which is filled with zeros, and I am trying to replace the elements of this array with either 'N' or 'A' based on whether the index of the array element is part of a separate vector called 'abnormal_cycles_I03'. I am currently using a 'for' loop as shown below, but this does not seem to be working. It just returns an array full of zeros. Any suggestions would be greatly appreciated, thanks in advance.
labels_I03 = categorical(zeros(1, number_cycles));
abnormal_cycles_I03 = [128];
for i = number_cycles - 1
if ~ismember(i, abnormal_cycles_I03)
labels_I03(i) = 'N';
else
labels_I03(i) = 'A';
end
end

채택된 답변

Adam Danz
Adam Danz 2021년 1월 23일
편집: Adam Danz 2021년 1월 26일
Sounds like you're describing this,
number_cycles = 10;
labels_I03 = categorical(repmat({'N'},1,number_cycles)); % set all as N
abnormal_cycles_I03 = [2,4,9]
abnormal_cycles_I03 = 1×3
2 4 9
labels_I03(abnormal_cycles_I03) = categorical({'A'}) % fill in A's
labels_I03 = 1×10 categorical array
N A N A N N N N A N

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by