Arrays 在数组中的特定位置写入值
我有一个数组:Arrays 在数组中的特定位置写入值,arrays,matlab,Arrays,Matlab,我有一个数组: ab = 3 cd = 4 array = zeros(ab, cd); 这给了我们一个数组,看起来像: 0 0 0 0 0 0 0 0 0 0 0 0 现在我想用特定的值填充每一行。我有两个类,它们有两个属性,我互相减分 因此,第一行中的第一个条目应通过以下公式计算: xxx = class1{1}.property - class2{1}.property xxx =
ab = 3
cd = 4
array = zeros(ab, cd);
这给了我们一个数组,看起来像:
0 0 0 0
0 0 0 0
0 0 0 0
现在我想用特定的值填充每一行。我有两个类,它们有两个属性,我互相减分
因此,第一行中的第一个条目应通过以下公式计算:
xxx = class1{1}.property - class2{1}.property
xxx = class1{2}.property - class2{1}.property
第一行中的第二个条目应由以下人员填写:
xxx = class1{1}.property - class2{2}.property
第三项:
xxx = class1{1}.property - class2{3}.property
第二行中的第一个条目应通过以下公式计算:
xxx = class1{1}.property - class2{1}.property
xxx = class1{2}.property - class2{1}.property
我试过:
for cc = 1:ab
for hh = 1:cd
array(cc, hh) = class1{cc}.property - class2{hh}.property
end
end
然而,matlab一直告诉我:“下标赋值维度不匹配。”
我确实理解问题的含义,但不知道如何解决:/
编辑:
该数组应该如下所示:
(class1{1}.property - class2{1}.property) (class1{1}.property - class2{2}.property)
(class1{2}.property - class2{1}.property) (class1{2}.property - class 2{2}.property)
正如在评论中所说的,如果不了解更多的代码,就很难知道,但这可能是因为一个或多个类属性具有多个值。如果类属性大于1值,则可以尝试以下操作来捕获
for cc = 1:ab
for hh = 1:cd
temp = class1{cc}.property - class2{hh}.property;
if length(temp) > 1
keyboard
end
array(cc, hh) = temp;
end
end
维度不匹配错误是由于我向每个数组条目添加了向量(class1.property和class2.property都是向量!) 如果不了解
class1{…}.property
就很难回答。如何生成这些变量?寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。有关class1属性的值在类1中生成,而类2属性的值在main中生成并传递给类@schorsch:我提到了错误,期望的行为,我试着给你一个代码示例,它象征着期望的函数。但是,感谢您的链接。在不知道class1{1}.property
等包含什么的情况下,正如前面提到的,代码是不可运行的-因此我们没有“在问题本身中重现(错误)所需的最短代码”。嗯,可能问题的原因是class1.property和class2.property都是向量?现在我使用了norm(class1{1}.property-class2{1}.property),代码运行得很好。不过,多亏你提到了长度,我有了这个想法:)谢谢你,伙计!