简体   繁体   English

如何摆脱这个MATLAB错误? “在赋值A(I)= B中,B和I中的元素数必须相同。”

[英]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.

相关问题 请帮助:在分配A(I)= B中,B和I中的元素数必须相同。 的MATLAB - Please Help: In an assignment A(I) = B, the number of elements in B and I must be the same. MATLAB 不知道如何纠正:“在赋值A(I)= B中,B和I中的元素数必须相同。” - Don't know how to Correct : “In an assignment A(I) = B, the number of elements in B and I must be the same.” 马赛克:??? 在赋值 A(I) = B 中,B 和 I 中的元素个数必须相同 - MATLAB: ??? In an assignment A(I) = B, the number of elements in B and I must be the same 赋值A(I)= B时出错,B和I中的元素数必须相同 - error with an assignment A(I) = B, the number of elements in B and I must be the same MATLAB:错误:在A(I)= B中,B和I中的元素数必须相同 - MATLAB: error: In A(I) = B, the number of elements in B and I must be the same 在分配A(I)= B中,B和I中的元素数必须相同 - In assignment A(I) = B, the number of elements in B and I must be the same 在分配A(I)= B中,B和I中的元素数必须相同 - In an assignment A(I) = B, the number of elements in B and I must be the same 在赋值A(I)= B中,B和I中的元素数必须相同 - In an assignment A(I) = B, the number of elements in B and I must be the same 如何解决:在分配A(I)= B中,B和I中的元素数必须相同 - How to solve: In an assignment A(I) = B, the number of elements in B and I must be the same 错误(B和我中的元素数必须相同) - Error (the number of elements in B and I must be the same)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM