new_img is ==>
new_img = = >
new_img = zeros(height, width, 3);
curMean is like this: [double, double, double]
curMean就像这样:[double, double, double]
new_img(rows,cols,:) = curMean;
so what is wrong here?
那么,这里有什么问题呢?
2 个解决方案
#1
3
The line:
线:
new_img(rows,cols,:) = curMean;
will only work if rows
and cols
are scalar values. If they are vectors, there are a few options you have to perform the assignment correctly depending on exactly what sort of assignment you are doing. As Jonas mentions in his answer, you can either assign values for every pairwise combination of indices in rows
and cols
, or you can assign values for each pair [rows(i),cols(i)]
. For the case where you are assigning values for every pairwise combination, here are a couple of the ways you can do it:
只有行和cols是标量值时才会工作。如果它们是向量,那么有一些选项你必须正确地执行任务,这取决于你所做的是什么类型的作业。正如Jonas在他的回答中提到的那样,您可以为行和cols中的每个索引组合分配值,或者您可以为每一对(i)、cols(i)分配值。对于为每一个成对组合分配值的情况,这里有几种方法:
-
Break up the assignment into 3 steps, one for each plane in the third dimension:
将任务分解为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
You could also do this in a for loop as Jonas suggested, but for such a small number of iterations I kinda like to use an "unrolled" version like above.
您也可以像Jonas建议的那样在for循环中执行这个操作,但是对于这样少量的迭代,我喜欢使用像上面这样的“unroll”版本。
-
Use the functions RESHAPE and REPMAT on
curMean
to reshape and replicate the vector so that it matches the dimensions of the sub-indexed section ofnew_img
:在curMean上使用功能重塑和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]);
For an example of how the above works, let's say I have the following:
举例来说,我有如下的例子:
new_img = zeros(3,3,3);
rows = [1 2];
cols = [1 2];
curMean = [1 2 3];
Either of the above solutions will give you this result:
以上任何一种解决方案都会给你这个结果:
>> 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
#2
2
Be careful with such assignments!
小心这些作业!
a=zeros(3);
a([1 3],[1 3]) = 1
a =
1 0 1
0 0 0
1 0 1
In other words, you assign all combinations of row and column indices. If that's what you want, writing
换句话说,您分配了所有行和列索引的组合。如果这就是你想要的,那就写吧。
for z = 1:3
newImg(rows,cols,z) = curMean(z);
end
should get what you want (as @gnovice suggested).
应该得到您想要的(正如@ g初学者所建议的)。
However, if rows
and cols
are matched pairs (i.e. you'd only want to assign 1 to elements (1,1)
and (3,3)
in the above example), you may be better off writing
但是,如果行和cols是匹配的(例如,您只希望将1赋给元素(1、1)和(3,3)),那么您可能会更好地编写。
for i=1:length(rows)
newImg(rows(i),cols(i),:) = curMean;
end
#1
3
The line:
线:
new_img(rows,cols,:) = curMean;
will only work if rows
and cols
are scalar values. If they are vectors, there are a few options you have to perform the assignment correctly depending on exactly what sort of assignment you are doing. As Jonas mentions in his answer, you can either assign values for every pairwise combination of indices in rows
and cols
, or you can assign values for each pair [rows(i),cols(i)]
. For the case where you are assigning values for every pairwise combination, here are a couple of the ways you can do it:
只有行和cols是标量值时才会工作。如果它们是向量,那么有一些选项你必须正确地执行任务,这取决于你所做的是什么类型的作业。正如Jonas在他的回答中提到的那样,您可以为行和cols中的每个索引组合分配值,或者您可以为每一对(i)、cols(i)分配值。对于为每一个成对组合分配值的情况,这里有几种方法:
-
Break up the assignment into 3 steps, one for each plane in the third dimension:
将任务分解为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
You could also do this in a for loop as Jonas suggested, but for such a small number of iterations I kinda like to use an "unrolled" version like above.
您也可以像Jonas建议的那样在for循环中执行这个操作,但是对于这样少量的迭代,我喜欢使用像上面这样的“unroll”版本。
-
Use the functions RESHAPE and REPMAT on
curMean
to reshape and replicate the vector so that it matches the dimensions of the sub-indexed section ofnew_img
:在curMean上使用功能重塑和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]);
For an example of how the above works, let's say I have the following:
举例来说,我有如下的例子:
new_img = zeros(3,3,3);
rows = [1 2];
cols = [1 2];
curMean = [1 2 3];
Either of the above solutions will give you this result:
以上任何一种解决方案都会给你这个结果:
>> 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
#2
2
Be careful with such assignments!
小心这些作业!
a=zeros(3);
a([1 3],[1 3]) = 1
a =
1 0 1
0 0 0
1 0 1
In other words, you assign all combinations of row and column indices. If that's what you want, writing
换句话说,您分配了所有行和列索引的组合。如果这就是你想要的,那就写吧。
for z = 1:3
newImg(rows,cols,z) = curMean(z);
end
should get what you want (as @gnovice suggested).
应该得到您想要的(正如@ g初学者所建议的)。
However, if rows
and cols
are matched pairs (i.e. you'd only want to assign 1 to elements (1,1)
and (3,3)
in the above example), you may be better off writing
但是,如果行和cols是匹配的(例如,您只希望将1赋给元素(1、1)和(3,3)),那么您可能会更好地编写。
for i=1:length(rows)
newImg(rows(i),cols(i),:) = curMean;
end