Getting values from 4d matrix according (using) 2d matrix with logical values (0 or 1) - does not work as wanted

2 visualizzazioni (ultimi 30 giorni)
Hello!
Let say I have a matrix A of dimensions (50*100*248*20) with data and I have another one B (50*100) with logical values (lets image I have 100 of "1") and I want to get values from my 4D matrix according to B. So I am doing
C = A(B);
But then I am getting my values only from A (:,:,1,1) - first page, but I am expecting to get values from all the pages (along all the 3rd and 4 th dimensions). How can I do this?
So I need at the output a matrix with dimensions (100, 1, 248, 20) - which give me 100 values from each page of 248*20
If I am not clear, let me know!, Hoping for you help! Thanks!
  6 Commenti
madhan ravi
madhan ravi il 4 Apr 2019
Modificato: madhan ravi il 4 Apr 2019
Indeed sir Walter, thank you for the explanation just noticed the comment.
Linus Olofsson
Linus Olofsson il 24 Giu 2020
Modificato: Linus Olofsson il 24 Giu 2020
Could someone please give an example of the explanation Walter gave above? I do not really understand how this is a solution of the problem, nor do I understand the code snippet provided by the earlier comment by Walter.
Could the solution please be explained with my very similar non-functional problem with a 3 channel color image and a 2 dimensional logical array (mask) that I would like to use to color code speficic parts of my image, i.e.:
image = zeros(150,5370,3);
whos mask
Name Size Bytes Class Attributes
mask 150x5370 805500 logical
image(mask,2) = 255; % Change the masked part of the image to be green.

Accedi per commentare.

Risposta accettata

Yurii Iotov
Yurii Iotov il 29 Mar 2019
Thank you guys for being active and for your help!
What I would like to do also and kindly ask you to help is how to ''cut'' and keep the values in a matrix ''form''? I mean to get a values which corresponds to logical 1, to put them in a matrix and at this matrix will stay some neighboring ''non-desired values'' (which corresponds to 0, since we are cutting as a 'rectangular') but to replace this values to NaN. Primitive illustration:
B = [0 0 0 0 0
0 1 1 1 0 => B1 = []
1 1 1 0 0
0 0 1 0 0]
.
=> B1 = 0 1 1 1 => C = A(B1)
1 1 1 0
0 0 1 0
.
A = NaN Val Val Val
Val Val Val NaN
NaN NaN Val NaN
Finally to plot A using pcolor or contour and to get smth like this (white zone represents these logical zeros, while plotted area logical ones and corresponding values)
Снимок.JPG
Sorry if not clear...Thanks
  3 Commenti
Yurii Iotov
Yurii Iotov il 29 Mar 2019
Zeros can only remains at the edges... logical 1s represent discretized (according to grid) shape of poligons (for example imagine hexagone or other complex shape)....Then from my 4d matrices with data I cut new ones with data which are inside this polygons (logical ones) and then I need to plot (colormap...etc) this data, but the ammount of zeros which was cut also I want to replace as NaN
Thanks
Walter Roberson
Walter Roberson il 29 Mar 2019
I think you mean that Zeros are only to be removed at the edges.
The code I posted here finds the bounding box that contains all of the 1's. It could easily be extended to more dimensions.

Accedi per commentare.

Più risposte (1)

Darren Wethington
Darren Wethington il 29 Mar 2019
See if this is what you're looking for:
A = rand(10,20,30,40);
B = round(rand(10,20));
four_dim_logical = zeros(size(A));
for i=1:length(four_dim_logical(1,1,:,1))
for j=1:length(four_dim_logical(1,1,1,:))
four_dim_logical(:,:,i,j) = B;
end
end
C = A(logical(four_dim_logical));
dim1 = sum(B(:));
dim2 = 1;
dim3 = size(A,3);
dim4 = size(A,4);
C = reshape(C,dim1,dim2,dim3,dim4);

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by