Replacing values in a logical vector based on a rule

8 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by