How to use logical indexing for slices of a 3D matrix
Afficher commentaires plus anciens
Hi guys, I am trying to set value for different page of 3D matrix based on 2D matrix logical command.
domain=rand(4,4,4);
domain2D=domain(:,:,1);
condition1=domain2D>0.5;%logical index all elements of first slice that greater than 0.5
condition2=domain2D<0.2;%logical index all elements of first slice that smaller than 0.2
domain2D(condition)=10;%assign all elements that greater than 0.5 a value of 10.
domain2D(condition2)=5;%assign all elements that smaller than 0.2 a value of 5.
After running the above code, we get the following
condition1 =
4×4 logical array
1 0 1 0
1 0 0 0
1 1 1 1
0 0 0 0
>> condition2
condition2 =
4×4 logical array
0 1 0 1
0 0 0 0
0 0 0 0
1 0 0 0
As you can see, we can easily apply condition1 and condition2 for a 2D array, which is 1st slice of the 3D array, ie domain(:,:,1). Now, based on the condition1 and condition2, I like to use these logical indexes for any slices of the 3D array that i want. In this example, each elements falls within condition 1 a value of 10 for slice 1 to 3, and each elements falls within condition 2 a value of 5 from slice 1 to 2 of the 3D array. You can visualize the results as below picture.

If we are working with 2D array, it would be very simple as: domain(condition1)=10 (assume domain is 2D). How can we use this logical function in 3D array for any slices that I would like to?. Please help. Thank you all.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!