在赋值 A(:) = B 中,A 和 B 中的元素数量必须相同 [英] In an assignment A(:) = B, the number of elements in A and B must be the same

查看:43
本文介绍了在赋值 A(:) = B 中,A 和 B 中的元素数量必须相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如尝试运行我的代码时

When trying to run my code, for example

for ii= 1:10
   output(ii)=rand(3);
end

我收到错误

In an assignment  A(:) = B, the number of elements in A and B must be the same

In an assignment  A(I) = B, the number of elements in B and I must be the same.

这个错误是什么意思?摆脱它的方法是什么?

What does this error mean? What is the approach to get rid of it?

推荐答案

出现此错误是因为您试图用比其大小多(或少)的值填充变量块.换句话说,你有一个声明 A(:)=B 关于 size(A(:))size(B) 不同的地方>.

This error comes because you are trying to fill a variable chunk with more (or less) values than its size. In other words, you have a statement A(:)=B on where size(A(:)) is different to size(B).

在问题的示例中,rand(3) 返回一个 3x3 矩阵,但是,output(ii) 只是一个值(即使 output 可能更大,output(ii) 只是 output 的单个值),因此 返回的值>rand(3) 不适合 output.

In the example in the question, rand(3) returns a 3x3 matrix, however, output(ii) is just a single value (even if output may be bigger, output(ii) is just a single value of output), thus the value returned by rand(3) does not fit inside output.

为了解决这个问题,你需要改变output变量的大小,这样你就有空间来容纳所有的结果.

In order to solve this problem, you need to change the the size of the output variable, so you have space to fit all the result.

有两种方法可以做到这一点.其中之一是创建一个适合回报的矩阵,例如output=zeros(3,3,10).

There are 2 ways of doing this. One of them is by creating a Matrix that fits the return, e.g. output=zeros(3,3,10).

然后我们可以把代码改成

Then we can change the code to

for ii= 1:10
   output(:,:,ii)=rand(3);
end

或者,您可以将output 填充为元胞数组.当函数的返回值每次都改变大小时,这特别有用,例如rand(ii);

Alternatively, you can fill the output as a cell array. This is particularly useful when the return of the function changes sizes each time, e.g. rand(ii);

在这种情况下,以下内容将起作用

In that case, the following would work

for ii= 1:10
   output{ii}=rand(ii);
end

<小时>

很可能与问题中的示例不同,在实际情况下,您不知道输出返回的大小,因此您不知道使用两个选项中的哪一个来修复您的代码.


It is probable that unlike in the example in the question, in the real case you do not know the size of what the output returns, thus you do not know which of the two options to use to fix your code.

了解这一点的可能方法是在代码出错时激活调试帮助,方法是在命令行中键入 dbstop if error.这将在 MATLAB 抛出错误时触发调试停止,您可以键入 size(rand(ii))size(output(ii)) 以查看大小两个都.

On possible way of learning that, is activating debugging help when the code errors, by typing dbstop if error in your command line. This will trigger a debugging stop when MATLAB throws an error, and you can type size(rand(ii)) and size(output(ii)) to see the sizes of both.

通常,阅读正在使用的函数的文档也有帮助,看看是否可以使用不同的大小.

Often, reading the documentation of the function being used also helps, to see if different sizes are possible.

也就是说,第二个选项,元胞数组,将始终确保一切都适合.然而,矩阵在 MATLAB 中通常更快更容易使用,因此如果可以,您应该瞄准基于矩阵的解决方案.

That said, the second option, cell arrays, will always ensure everything will fit. However matrices are generally a faster and easier to use in MATLAB, thus you should aim for the matrix based solution if you can.

这篇关于在赋值 A(:) = B 中,A 和 B 中的元素数量必须相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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