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

查看:3047
本文介绍了在赋值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(:))是不同于尺寸(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 矩阵,但是,输出(ii)只是一个值(即使输出可能是更大,输出(ii)只是输出的单个值,因此<$ c $返回的值c> rand(3)不适合输出

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.

为了解决这个问题,你需要改变输出变量的大小,所以你有空间以适应所有结果。

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.

有两种方法可以做到这一点。其中之一是创建适合回报的矩阵,例如: 输出=零(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

或者,您可以填写输出作为单元格数组。当函数的返回每次改变大小时,这尤其有用,例如, 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(输出(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天全站免登陆