Replacing values in a logical vector based on a rule
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Stephen23
le 16 Avr 2020
Modifié(e) : Stephen23
le 16 Avr 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)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!