new_img是==>MATLAB:如何解决下标赋值维度不匹配问题?
new_img = zeros(height, width, 3);
curMean是这样的:[double, double, double]
new_img(rows,cols,:) = curMean;
所以这里有什么问题?
new_img是==>MATLAB:如何解决下标赋值维度不匹配问题?
new_img = zeros(height, width, 3);
curMean是这样的:[double, double, double]
new_img(rows,cols,:) = curMean;
所以这里有什么问题?
行:
new_img(rows,cols,:) = curMean;
如果rows
和cols
是标值才有效。如果它们是矢量,那么根据您正在进行的分配的具体情况,您需要执行一些选项才能正确执行分配。由于Jonas mentions in his answer,您可以为rows
和cols
中的每个成对索引组合分配值,也可以为每对[rows(i),cols(i)]
分配值。对于你在哪里,每两两组合分配值的情况下,这里有一对夫妇的方法可以做到这一点:
分配分解成3个步骤,一个是在第三维度中的每个平面:
new_img(rows,cols,1) = curMean(1); %# Assignment for the first plane
new_img(rows,cols,2) = curMean(2); %# Assignment for the second plane
new_img(rows,cols,3) = curMean(3); %# Assignment for the third plane
你也可以做到这一点的for循环Jonas suggested,但对于这么小的迭代次数我还挺喜欢用一个“展开的”版本像上面。
使用上curMean
功能RESHAPE和REPMAT重塑和复制载体中,使得它的new_img
子索引截面的尺寸相匹配:
nRows = numel(rows); %# The number of indices in rows
nCols = numel(cols); %# The number of indices in cols
new_img(rows,cols,:) = repmat(reshape(curMean,[1 1 3]),[nRows nCols]);
有关如何的一个例子上面的作品,让我们说我有以下几点:
new_img = zeros(3,3,3);
rows = [1 2];
cols = [1 2];
curMean = [1 2 3];
无论是abov e解决方案会给你这个结果:
>> new_img
new_img(:,:,1) =
1 1 0
1 1 0
0 0 0
new_img(:,:,2) =
2 2 0
2 2 0
0 0 0
new_img(:,:,3) =
3 3 0
3 3 0
0 0 0
要小心这样的任务!
a=zeros(3);
a([1 3],[1 3]) = 1
a =
1 0 1
0 0 0
1 0 1
换句话说,您分配所有行和列索引的组合。如果这是你想要的东西,写
for z = 1:3
newImg(rows,cols,z) = curMean(z);
end
应该得到你想要的东西(如@gnovice建议)。
然而,如果rows
和cols
匹配对(即你只要分配1到元件(1,1)
和(3,3)
在上面的例子中),则可能会更好写入
for i=1:length(rows)
newImg(rows(i),cols(i),:) = curMean;
end
假设行列数在循环内部,并且赋值给curMean并赋值给curMean,然后应该没有错误,curMean被分配给z/j(无论你想调用它)是多维数组的一部分。 – phwd 2011-01-25 17:31:27
行和列是每个索引列表。 = \ – NullVoxPopuli 2011-01-25 17:33:36