简体   繁体   中英

MATLAB: Creating an Array/Vector from one dimension of a 4D matrix.

I have a 4D vector with size(N_square) = (64,32,87,1460) And I need to filter the overall N_square for values less than 0. However, I then need to take the pressure values(3rd Dimension) corresponding to those N^2 < 0 values and make a histogram of them. I have already figured out how to filter the values of N^2 the way I want to, now I am trying to grab that 3rd dimension with the correct values and make a new matrix with just the pressure values. Here is my code that filter the N^2.

N_square(N_square > 0) = 0);
N_square = abs(N_square);

The problem with this part as well is that I end up with a vector of all the values I want, and then a crap ton of zeros.

Any thoughts or ideas? I really need to get this done soon.

Does this solve your problem?

X = ones(2,3,4,5);

X(:,:,2,:) = -1;
X(:,:,4,:) = -1;
X(1,1,3,1) = -1;
X(1,1,1,1) = -1;

[I1,I2,I3,I4]=ind2sub(size(X), find(X<0));

hist(I3)

MATLAB matrices have two kind of indices, subscript and linear indices . Subscript indices are the ones you are used to like X(i1,i2,i3,i4) but you could also write X(k) which is a linear index. Calling find(X<0) gives the linear indices of all entries in X less than zero. ind2sub() converts the linear to subscript indices. hist(I3) then plots the distribution of the third index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM