How to use logical indexing for slices of a 3D matrix
78 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/718044/image.jpeg)
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.
0 Comments
Accepted Answer
Adam Danz
on 22 Aug 2021
Edited: Adam Danz
on 22 Aug 2021
nxm logical index applied to all pages of a 3D array
domain = rand(4,4,4);
condition1 = domain(:,:,1).*ones(size(domain)) > 0.5;
condition2 = domain(:,:,1).*ones(size(domain)) < 0.2;
domain(condition1) = 10;
domain(condition2) = 5
nxm logical indexing applied to specific pages of a 3D array
% tf is a n*m logical matrix
% pg is a row vector of pages
% output: matrix of linear indices
get3DInd = @(tf,pg)find(tf) + numel(tf)*(pg-1);
domain = rand(4,4,4);
condition1 = domain(:,:,1) > 0.5;
% Apply to pages 1 and 3
domain(get3DInd(condition1,[1,3])) = 10
5 Comments
Adam Danz
on 22 Aug 2021
Edited: Adam Danz
on 22 Aug 2021
Sure, get3DInd is an anonymous function with two inputs (tf, pg) and an output. tf is "condition" matrices which are n*m logical matrices that identify the rows and columns of the 3D array. pg are the page indices. It is assumed that the size of tf is the same size as the first two dimensions for the 3D array.
The purpose of the function is to produce a list of linear indices of a 3D array based on the rows and columns identified in tf and the page indices identified in pg.
Don't forget that values in Matlab are counted top-to-bottom, left-to-right, and then by pages (see previous link).
Example of linear index:
a = randi(100,2,4,3)
linearIdx = [3 10 14 21]; % 3rd, 10th, 14th, and 21st value of 'a'
a(linearIdx)
Example of get3DInd
get3DInd = @(tf,pg)find(tf) + numel(tf)*(pg-1);
a = rand(3,3,3) % 3D array
tf = logical([0 1 0; 1 1 1; 0 1 0])
% Create linear indices
% The pattern in tf will be applied to pages 1 and 3
linIdx = get3DInd(tf, [1,3])
a(linIdx) = NaN
As you can see, the pattern identified in tf has been applied to the 1st and 3rd page of the 3D array 'a'.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!