分配比Matlab中的非单个下标错误具有更多的非单个rhs维度 [英] Assignment has more non-singleton rhs dimensions than non-singleton subscripts error in matlab

查看:99
本文介绍了分配比Matlab中的非单个下标错误具有更多的非单个rhs维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在MATLAB中看到我的部分代码.当我运行它时,我收到此错误: 分配具有比非单身更大的非单身rhs尺寸 下标. 请帮助我纠正它,并知道其原因. tnx很多.

U can see a part of my code in MATLAB. when i run it, i receive this error: Assignment has more non-singleton rhs dimensions than non-singleton subscripts. please help me to correct it and know it's reasons. tnx a lot.

while Time < t_stop

for i4 = 1:x
for j4 = 1:y
for k4 = 1:z

% t1 has been defined in another place and updates in every cycle
t2 = find(t1 ~= -1); %t2 is not empty because t1 has non-(-1) elements
t3 = zeros(1,numel(t2));
for i5 = 1:numel(t2)
t3(i5) = t1(t2(i5));
end;
var1 = min(t3(:));
min_time(i4,j4,k4) = var1;
if numel(find(t1 == var1)) == 1
    min_IND (i4,j4,k4) = find(t1 == var1);
else
    Temp_find = find(t1 == var1);
    min_IND (i4,j4,k4) = Temp_find(randi(numel(find(t1 == var1))));
end;

t1 = zeros(1,41)-1;
end;
end;
end;

Time=Time+1;

end;

推荐答案

为什么会发生错误?

这是由于您的t2 = find(t1 ~= -1)语句返回了一个空数组.这意味着您的t1数组没有任何不等于-1的条目.在开始编写代码时,由于早先的find调用,t3 = zeros(1,numel(t2))中的元素数为0,因此t3 = zeros(1,numel(t2))将创建一个空矩阵.

Why is your error happening?

This is due to the fact that your t2 = find(t1 ~= -1) statement returns an empty array. This means that your t1 array does not have any entries that are not equal to -1. As you start going down into your code, t3 = zeros(1,numel(t2)) will create an empty matrix as the number of elements in t2 are 0 due to the find call from earlier.

接下来,当您看到以下语句时:

Next, when you get to this statement:

for i5 = 1:numel(t2)
    t3(i5) = t1(t2(i5));
end

for循环不会执行,因为t2中的元素数等于0.

This for loop does not execute as the number of elements in t2 equals 0. When you get to this statement after:

var1 = min(t3(:));

var1也将为您提供一个空矩阵.最后,当您得到以下语句时:

var1 will also give you an empty matrix. Finally, when you get to this statement:

min_time(i4,j4,k4) = var1;

您正在尝试将值分配给min_time中的位置.具体来说,您正在尝试为min_time中的一个位置分配一个值,该位置应存储在var1中,但是因为它为空,所以这就是为什么您会收到此错误的原因.因此,在继续执行代码之前,您需要仔细检查t2变量以确保该变量不为空.

You are trying to assign an empty value into a location in min_time. Specifically, you are trying to assign one value to a location in min_time which should be stored in var1, but because it's empty, this is why you are getting this error. As such, you need to double check your t2 variable to ensure that this isn't empty before you proceed with your code.

我可以看到为什么,并且在哪里出现此错误.如果您看一看代码的结尾,则有以下语句:

I can see why you are getting this error, and where this is happening. If you look towards the end of your code, you have this statement:

t1 = zeros(1,41)-1;

您正在做的是制作t1,以使条目为全部 -1.当您返回循环的下一个迭代时,由于t1 all 值等于-1,因此t2 = find(t1 ~= -1)语句将返回一个空数组.因为find找不到与-1 不相等的任何值,所以find返回一个空数组,指出它找不到t1中任何不等于<的位置. c2>.这可能不是您打算要做的.

What you are doing is you are making t1 such that the entries are all -1. When you go back into the next iteration of your loop, the t2 = find(t1 ~= -1) statement will return an empty array as all values of t1 are equal to -1. Because find could not find any values that are not equal to -1, find returns an empty array stating that it could not find any locations in t1 that were not equal to -1. This is probably not what you intended to do.

您的评论指出,在执行代码之前,已在for循环之外设置了t1.尽管可能是这种情况,但是您要在for循环的第一次迭代之后更改t1,这不可避免地会给您所看到的错误.

Your comment states that t1 is being set outside of the for loops before your code is executed. Though that may be the case, you are changing t1 after the first iteration of your for loops, which will inevitably give you that error you are seeing.

这样,您可能需要更正此语句-通过删除它,或对其进行修改以使其成为您要查找的定义.

As such, you probably need to correct this statement - either by removing it, or modifying it so that it is the definition that you are seeking.

如果您debug您的代码确实有用,那么您可以弄清楚发生了什么.因此,MATLAB具有一些强大的功能来帮助您调试代码,以便找出原因.如果要执行此操作,则需要将MATLAB设置为调试模式.如果您要这样做,请在MATLAB编辑器中转到看到t2 = find(t1 ~= -1);语句的行,然后单击行号旁边的水平破折号.您会看到一个红点,指示该功能运行时,该功能将在此时暂停.

It really helps if you debug your code so that you can figure out what's going on. As such, MATLAB has some great facilities to help you debug your code so you can figure out why it isn't working. You need to set MATLAB to debug mode if you'd like to do this. If this is something you want, in your MATLAB editor, go to the line where you see the t2 = find(t1 ~= -1); statement, and click on the horizontal dash beside the line number. You'll see a red dot indicating that when the function runs, the function will pause at this point.

debug模式下,您可以查看代码的当前执行状态.您还可以检查变量的外观,以及在此之后逐行逐步执行操作,以便可以看到每行在做什么.您可以在调试模式下输入dbstep以转到下一行,也可以在MATLAB编辑器中通过单击 Step 按钮来执行此操作.您还可以选择其他不同的按钮:

In debug mode, you are able to see what the current state of execution your code is in. You can also examine what the variables look like, as well as step through line by line after this point so you can see what each line is doing. You can either type in dbstep when you're in debug mode to go to the next line, or you can do this in your MATLAB editor by clicking on the Step buttons. There are also different buttons you can choose from:

  • 继续-继续运行代码,直到到达下一个断点或函数退出
  • 步骤-运行下一行代码
  • 进入-运行下一行代码.如果此行是 function ,那么您将进入该函数内部并能够在其中调试语句.如果您只是执行 Step ,它将运行该功能,但不会进入内部.因此,这将在暂停下一行之前运行整个功能.
  • 退出-如果您在函数内部,执行此操作将运行代码,直到函数离开.然后,代码将在您离开函数的那一点之后立即暂停,然后从那里继续.
  • Continue - Continue running your code until you reach the next breakpoint or the function exits
  • Step - Run the next line of code
  • Step In - Run the next line of code. If this line is a function, then you will go inside the function and be able to debug the statements within there. If you just do Step, it runs the function, but doesn't go inside. As such, this runs the entire function before pausing at the next line.
  • Step Out - If you are inside a function, doing this will run the code until the function leaves. The code will then pause right after the point where you leave the function and you continue from there.

有关MATLAB调试的更多信息,请在此处查看以下出色的MathWorks链接:

For more information on MATLAB debugging, check out this great MathWorks link here: http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-178

这篇关于分配比Matlab中的非单个下标错误具有更多的非单个rhs维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆