How can I rewrite this code as to ommit the for loop?

1 vue (au cours des 30 derniers jours)
charles
charles le 28 Juil 2017
Commenté : Star Strider le 28 Juil 2017
I'm writting a script for root (of plants) analysis. I work with quite large datasets (1000*1000*1000 pixels) and therefor I would like to optimalize the processing speed of the script. Right now I am using a for loop in order to assign a value of 1 to certain points in the volume with the rest being 0. My script looks as follows
V2 = zeros(dimx,dimy,dimz);
for x = 1:dimx
for y = 1:dimy
for z = 1:dimz
if labda3(x,y,z) > 0 ;
V2(x,y,z) = 0;
elseif labda2(x,y,z) > 0 ;
V2(x,y,z) = 0;
else V2(x,y,z) = 1;
end
end
end
end
Since Matlab is not very fast when using for loops I assumed that there is a faster way of doing this, however I cannot think of any myself. Can anyone help me with this?

Réponse acceptée

Star Strider
Star Strider le 28 Juil 2017
See if this does what you want:
labda2 = randn(5, 5, 2) % Create Data
labda3 = randn(5, 5, 2) % Create Data
V2 = ones(size(labda2));
V2((labda2>0) | (labda3>0)) = 0
If it does what you want with the test data, it should work with your larger matrix.
  2 commentaires
charles
charles le 28 Juil 2017
Thx! This does exactly what I want!
Star Strider
Star Strider le 28 Juil 2017
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by