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

1 Ansicht (letzte 30 Tage)
charles
charles am 28 Jul. 2017
Kommentiert: Star Strider am 28 Jul. 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?

Akzeptierte Antwort

Star Strider
Star Strider am 28 Jul. 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 Kommentare
charles
charles am 28 Jul. 2017
Thx! This does exactly what I want!
Star Strider
Star Strider am 28 Jul. 2017
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by