Effacer les filtres
Effacer les filtres

How to avoid for nested loops with if condition?

2 vues (au cours des 30 derniers jours)
Mantas Vaitonis
Mantas Vaitonis le 25 Juin 2018
Commenté : Mantas Vaitonis le 26 Juin 2018
Hello,
I have big matrixes of same size, and am suing for loop with if statement, which is bottleneck in my code and is very slow. How would it be possible to optimize it?
for i=1:n1
for j=1:n2
if id(i,j)==1
if RR(i,j)==1
id(i,x(i,j))=0;
end
end
end
end
Maybe it is possible to vectorize or use bsxfun?

Réponse acceptée

Guillaume
Guillaume le 25 Juin 2018
Modifié(e) : Guillaume le 25 Juin 2018
You certainly don't need the j loop:
for row = 1:size(id)
id(row, x(id(row, :) == 1 & RR(row, :) == 1)) = 0;
end
You could get rid of the i loop as well at the expense of memory (another array the same size as id)
mask = id == 1 & RR == 1; %and if id and RR are logical, simply: mask = id & RR;
rows = repmat((1:size(id, 1)', 1, size(id, 2));
id(sub2ind(size(id), rows(mask), x(mask))) = 0;
  10 commentaires
Guillaume
Guillaume le 26 Juin 2018
So it's replicated pairs in a single row that need to be removed, no replicated pairs across the whole matrix. If so, you can indeed use a loop over the rows as you've done but yours is overcomplicated and your transition through a 3d matrix only works by accident (your cat(3,...) is reshaped into a 2d matrix). This would be simpler:
x = [3 1 1 5 3;2 1 1 5 3];
a = repmat([1 2 3 4 5], size(x, 1), 1);
for row = 1:size(x, 1)
[~, tokeep] = unique(sort([x(row, :)', a(row, :)'], 'rows', 'stable');
a(row, setdiff(1:size(a, 2), tokeep) = 0;
end
Now, there is a way to do it without a loop. It's pretty hairy, having to go through a 4D matrix:
x = [3 1 1 5 3;2 1 1 5 3];
a = repmat([1 2 3 4 5], size(x, 1), 1);
sortedpairs = sort(cat(3, x, a), 3);
matchedpairs = all(sortedpairs == permute(sortedpairs, [1 4 3 2]), 3);
matchnotfirst = cumsum(matchedpairs, 2) > 1 & matchedpairs; %only keep 2nd and subsequent pair
toreset = any(matchnotfirst, 4);
a(toreset) = 0
Mantas Vaitonis
Mantas Vaitonis le 26 Juin 2018
Thank You, greate now it wokrs as it should, without for loop.

Connectez-vous pour commenter.

Plus de réponses (1)

Nithin Banka
Nithin Banka le 25 Juin 2018
I don't think you need to use 'for' loop. The 2 'if' statements can be easily handled in MATLAB.
index_of_1_in_id_and_RR = (id==1&RR==1); %valid as you told they have same size
You can use this variable in further steps.
  3 commentaires
Nithin Banka
Nithin Banka le 25 Juin 2018
Can you provide me 'x'?
Mantas Vaitonis
Mantas Vaitonis le 25 Juin 2018
Yes, for example all arrays are same size (5x5), RR,id and x. Values inside arrays can be in random 1:5 type double. Like x can be [5 3 1 1 2;....]

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands 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!

Translated by