new_img是==>
new_img = zeros(height, width, 3); curMean像这样: [double, double, double]
new_img(rows,cols,:) = curMean; 那么这是怎么了?
回答:
该行:
new_img(rows,cols,:) = curMean; 仅当rows和cols是标量值时才有效。如果它们是向量,则有几种选择必须正确执行分配,具体取决于您要执行的分配类型。正如
乔纳斯在他的回答提到 ,既可以分配值在索引的每一个成对组合rows和cols ,也可以对于每对赋值[rows(i),cols(i)] 。对于为每个成对组合分配值的情况,可以通过以下两种方法来实现:
- 将作业分为3个步骤,第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 您也可以按照Jonas的建议在for循环中执行此操作,但是对于如此少量的迭代,我有点想使用上面的“展开”版本。
- 使用功能RESHAPE和REPMAT上curMean重塑和复制载体中,使得它的子索引部分的尺寸相匹配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]; 上述任何一种解决方案都会为您提供以下结果:
>> 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
更多&回答...