Replacing values in a logical vector based on a rule

15 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 16 Apr 2020
Edited: Stephen23 on 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)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!