简体   繁体   English

索引超过矩阵尺寸错误

[英]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. 具体来说,就是当我们所称的值N ^ 2小于零时。 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. 接下来,我将使用4-D矩阵N_square并将所有大于零的值过滤为等于0。然后,将剩余的负值的绝对值作为绝对值。 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. 这个目标的目的是只丢弃所有N_square的值为0的值。我认为这是问题开始的地方。 Jumping down to the for loop, I am then taking the 3rd dimension of N_square and converting pressure to altitude. 跳到for循环,然后使用N_square的第3维并将压力转换为高度。

My concern is that when I run this, PresNew = squeeze(N_square(:,:,k,:)); 我担心的是,当我运行此代码时,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. 因此,MATLAB会将结果折叠为向量,并且您无法像尝试的那样索引向量的第三维。

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). 解决方案是索引编制后(即在循环内)删除0值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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