Replacing values in a logical vector based on a rule

조회 수: 2 (최근 30일)
Amy Hassett
Amy Hassett 2020년 4월 16일
편집: Stephen23 2020년 4월 16일
So I have a logical column vector, that I have transformed into a double (it is 21080x1). I am now trying to replace every "1" with the corresponding row value from a different array, but what I am getting out is: [0,1].
Any help would be greatly appreciated.
for n= 1:numel(Behaviours) %Behaviours is a 27x1 cell array
temp = strcmp(Group1_KOs.BehaviourType, Behaviours(n)); %creates the logical vector
temp = double(temp); %convert to a double (just in case)
if temp(i) == 1 %where temp(i) is 1, replace with:
temp(i) =Group1_KOs.BehaviourData(i,7); %value in i'th row, 7th column
end

채택된 답변

Stephen23
Stephen23 2020년 4월 16일
편집: Stephen23 2020년 4월 16일
Try using logical indexing rather than a loop:
idx = strcmp(Group1_KOs.BehaviourType, Behaviours(n));
tmp = double(idx);
tmp(idx) = Group1_KOs.BehaviourData(idx,7)
or alternatively just multiply the index by those values:
tmp = idx.*Group1_KOs.BehaviourData(:,7)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by