简体   繁体   中英

Index Exceeds Matrix Dimensions Error

I'm currently working on creating a histogram of Altitudes at which a type of atmospheric instability happens. To be specific, it is when the values of what we call, N^2 is less than zero. This is where the problem comes in. I am trying to plot the occurrence frequency against the altitudes.

load /data/matlabst/DavidBloom/N_square_Ri_number_2005.mat

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

k = (1:87);
H = 7.5;
p0 = 101325;
nbins = (500);

N_square(N_square==0)=[];
Alt = zeros(1,578594);
PresNew = squeeze(N_square(:,:,k,:));

for lati = 1:32
    for long = 1:64
        for t = 1:1460
            for k = 1:87
                Alt(1,:) = -log((PresNew)/p0)*H;
            end
        end
    end
end

So, let me explain what I am doing. I'm loading a file with all these different variables. Link To Image This shows the different variables it displays. Next, I take the 4-D matrix N_square and I filter all values greater than zero to equal 0. Then I take the absolute value of the leftover negative values. I then define several variables and move on to the next filtering.

(N_square(N_square==0)=[];

The goal of this one was give just discard all values of N_square that were 0. I think this is where the problem begins. Jumping down to the for loop, I am then taking the 3rd dimension of N_square and converting pressure to altitude.

My concern is that when I run this, PresNew = squeeze(N_square(:,:,k,:)); is giving me the error.

Error in PlottingN_2 (line 10) PresNew = squeeze(N_square(:,:,k,:));

And I have no idea why.

Any thoughts or suggestions on how I could avoid this catastrophe and make my code simpler? Thanks.

When you remove random elements from a multi-dimensional array, they are removed but it can no longer be a valid multi-dimensional array because it has holes in it. Because of this, MATLAB will collapse the result into a vector, and you can't index into the third dimension of a vector like you're trying.

data = magic(3);
%   8     1     6
%   3     5     7
%   4     9     2

% Remove all values < 2
data(data < 2) = []
%   8     3     4     5     9     6     7     2

data(2,3)
% Index exceeds matrix dimensions.

The solution is to remove the 0 values after your indexing (ie within your loop).

Alt = zeros(1,578594);
for lati = 1:32
    for long = 1:64
        for t = 1:1460
            for k = 1:87
                % Index into 4D matrix
                PresNew = N_square(:,:,k,:);

                % NOW remove the 0 values
                PresNew(PresNew == 0) = [];

                Alt(1,:) = -log((PresNew)/p0)*H;
            end
        end
    end
end

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