[英]How do I get rid of this MATLAB error? “In an assignment A(I) = B, the number of elements in B and I must be the same.”
I keep getting this error for the b array below 我不断收到以下b数组的错误
A = ones(numTrials, 2*numDOFs); A =个(numTrials,2 * numDOFs);
b = ones(numTrials, 1); b =个(numTrials,1);
k = 2;
for i = 2:numSubjects
for j = 1:numTrials
A(k,:) = cat(2,subject(i).trial(j).torque_integral, subject(i).trial(j).work_integral);
b(k) = [subject(i).trial(j).E - subject(i).trial(j).B - sum(subject(i).trial(j).work_integral)];
k = k + 1;
end
end
I tested each component of the creation of the b array (subject(i).trial(j).E, etc.) and they are all scalar values. 我测试了b数组创建的每个组成部分(subject(i).trial(j).E等),它们都是标量值。 So I should be able to add/subtract them then assign them to an index in the preallocated b array. 因此,我应该能够添加/减去它们,然后将它们分配给预分配的b数组中的索引。 A simple dummy version would look like this: 一个简单的虚拟版本如下所示:
c = ones(4,1) c =个(4,1)
c = c =
1
1
1
1
c(3) = 3 c(3)= 3
c = c =
1
1
3
1
So what am I missing here? 那我在这里想念什么? Thanks! 谢谢!
You should probably print out the size of every element that goes into the assignments, and your problem will solve itself; 您可能应该打印出分配给每个元素的大小,这样您的问题就会解决。
size(cat(2,subject(i).trial(j).torque_integral, subject(i).trial(j).work_integral))
size([subject(i).trial(j).E - subject(i).trial(j).B - sum(subject(i).trial(j).work_integral)])
Note - I am leaving off the trailing ;
注意-我不用拖尾了;
in the above, which causes the value to be output to the console; 在上面,这导致该值被输出到控制台; also, your assignment to B has []
around it, as though you are trying to create a vector. 同样,您对B的赋值周围有[]
,就好像您要创建向量一样。 If it's truly a single element there is no need for that... 如果它确实是单个元素,则不需要...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.