Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-14, 20:13   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 MATLAB:如何解决下标的作业尺寸不匹配?

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循环中执行此操作,但是对于如此少量的迭代,我有点想使用上面的“展开”版本。
  • 使用功能RESHAPEREPMAT上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

更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 19:43


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