MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MATLAB:如何解决下标的作业尺寸不匹配? (https://www.labfans.com/bbs/showthread.php?t=26222)

poster 2019-12-14 20:13

MATLAB:如何解决下标的作业尺寸不匹配?
 
new_img是==>

new_img = zeros(height, width, 3); curMean像这样: [double, double, double]

new_img(rows,cols,:) = curMean; 那么这是怎么了?



[B]回答:[/B]

该行:

new_img(rows,cols,:) = curMean; 仅当rows和cols是标量值时才有效。如果它们是向量,则有几种选择必须正确执行分配,具体取决于您要执行的分配类型。正如[URL="https://stackoverflow.com/questions/4796729/matlab-how-do-i-fix-subscripted-assignment-dimension-mismatch/4796958#4796958"]乔纳斯在他的回答提到[/URL] ,既可以分配值在索引的每一个成对组合rows和cols ,也可以对于每对赋值[rows(i),cols(i)] 。对于为每个成对组合分配值的情况,可以通过以下两种方法来实现:
[LIST][*]将作业分为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 您也可以按照[URL="https://stackoverflow.com/questions/4796729/matlab-how-do-i-fix-subscripted-assignment-dimension-mismatch/4796958#4796958"]Jonas的建议[/URL]在for循环中执行此操作,但是对于如此少量的迭代,我有点想使用上面的“展开”版本。
[*]使用功能[URL="http://www.mathworks.com/help/techdoc/ref/reshape.html"]RESHAPE[/URL]和[URL="http://www.mathworks.com/help/techdoc/ref/repmat.html"]REPMAT[/URL]上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]);[/LIST]有关上述工作原理的示例,假设我有以下内容:

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

[url=https://stackoverflow.com/questions/4796729]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:22

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.