Getting values from 4d matrix according (using) 2d matrix with logical values (0 or 1) - does not work as wanted
Afficher commentaires plus anciens
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 commentaires
madhan ravi
le 29 Mar 2019
A(B,:,:) % totally a hunch
Walter Roberson
le 29 Mar 2019
temp = reshape(A, size(A,1)*size(A,2), 1, size(A,3), size(A,4));
C = temp(B,:,:,:);
Walter Roberson
le 29 Mar 2019
madhan, unfortunately your hunch will not work. B will effectively be reshaped, as if you had asked for A(B(:),:,:) . So the 50*100 B will act like a 5000 long logical index in the first dimension of A. That will fail if B contains any "true" value past the 50th entry, as it would be an index out of range.
The situation would have been different if you were using B as the last index, something like A(:,:,B) . When you pass in fewer index locations than there are dimensions, then MATLAB acts as-if the remaining dimensions were reshaped, as if you were accessing
reshape(A, size(A,1), size(A,2), size(A,3)*size(A,4)) indexed at (:,:,B(:))
Another way of saying that is that MATLAB uses linear index calculations for the last index that you provide, but not for the earlier ones.
Yurii Iotov
le 29 Mar 2019
madhan ravi
le 4 Avr 2019
Modifié(e) : madhan ravi
le 4 Avr 2019
Indeed sir Walter, thank you for the explanation just noticed the comment.
Linus Olofsson
le 24 Juin 2020
Modifié(e) : Linus Olofsson
le 24 Juin 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.
Réponse acceptée
Plus de réponses (1)
Darren Wethington
le 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);
2 commentaires
Darren Wethington
le 29 Mar 2019
Just saw solutions in the comments. This solution will be MUCH more computationally expensive if either of those work.
Yurii Iotov
le 29 Mar 2019
Catégories
En savoir plus sur Line Plots 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!
