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

1 visualización (últimos 30 días)
charles
charles el 28 de Jul. de 2017
Comentada: Star Strider el 28 de Jul. de 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?

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by