无法执行分配,因为左侧的索引与右侧的大小不兼容. [英] Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.?

查看:996
本文介绍了无法执行分配,因为左侧的索引与右侧的大小不兼容.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F=[125 250 500 1000 2000 4000];
T = [1.1 1.0 0.85 0.8 0.65 0.55]
V = 4*5*2.8;
L_n = [76 78 81 84 83 80];
L_f = [80 81 86 87 86 82];
fi = [1];
fi(end+1) = 0.8*(V./T)

我想将值添加到fi中,但是出现错误由于左侧的索引与大小不兼容,因此无法执行赋值"在右侧."

I want to add the values in to fi however i get the eror "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."

推荐答案

Matlab 一维以上的矩阵对于一个维度中的每个值,必须具有相同数量的元素.

Matlab matrices with more than 1 dimesion must have same number of elements for each value in one of the dimensions.

在您的情况下,当声明 fi 时,其尺寸为 1x1 .并且您试图在第一个维度的第二个位置上添加6个元素.因此,如果这行得通,您将得到如下结果: [[1],[a1 a2 a3 a4 a5 a6]] .但这是不允许的.

In your case, when fi is declared its dimension is 1x1. And you are trying to add 6 elements on the 2nd position of the first dimension. So, if that worked, you would end up with something like this: [[1],[a1 a2 a3 a4 a5 a6]]. But that is not allowed.

如果您仍然想这样做,则可能要使用

If you still want to do that, you may want to use a cell array. Which allows you to add different elements.

这里有个例子:

fi = {1}
fi{end+1} = 0.8*(V./T)

请注意, sum 之类的某些功能将无法在单元格数组上运行,因此您必须进行一些转换.

Notice that some functions like sum won't work on a cell array and you would have to do some conversions.

另一方面,如果您希望得到类似以下的输出: [1 a1 a2 a3 a4 a5 a6] (例如在另一个答案上加起来).您可能有不同的选项包括 horzcat :

On the other hand if you expected to have a different output like this: [1 a1 a2 a3 a4 a5 a6] (like sugested on another answer). You may have different options including horzcat:

fi = horzcat([1],0.8*(V./T))

这篇关于无法执行分配,因为左侧的索引与右侧的大小不兼容.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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