Replacing values in a logical vector based on a rule

1 Ansicht (letzte 30 Tage)
Amy Hassett
Amy Hassett am 16 Apr. 2020
Bearbeitet: Stephen23 am 16 Apr. 2020
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

Akzeptierte Antwort

Stephen23
Stephen23 am 16 Apr. 2020
Bearbeitet: Stephen23 am 16 Apr. 2020
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)

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by