How to vectorize this code to eliminate nested For loops?
1 次查看(过去 30 天)
显示 更早的评论
Would like to know how this code be vectorized:
for y=1:rows
for x=1:cols
nEnergy(y,x) = (Energy(y,x) - min(Energy)) / (max(Energy) - min(Energy));
end
end
for y=1:rows
for x=1:cols
weight(y,x) = dot(nEnergy(y,x), delta(y,x));
end
end
nEnergy, weight, delta is an array.
1 个评论
Stephen23
2018-9-4
编辑:Stephen23
2018-9-4
@Jae Min Lee: are you sure that this line really does what you need?:
nEnergy(y,x) = (Energy(y,x) - min(Energy)) / (max(Energy) - min(Energy));
It looks like an attempt at normalization, but if Energy is a matrix then that syntax of min and max will operate over the first dimension, giving row vectors as outputs. As then both inputs to mrdivide are row vectors of the same size, your code is actually solving a set of linear equations, which will return a scalar... but certainly NOT the normalized value! Is this the intended effect?
Are you sure that this line does what you need?:
dot(nEnergy(y,x), delta(y,x));
The dot product of scalars is just the multiple of those scalar values. Is this the intended effect?
采纳的回答
更多回答(1 个)
KALYAN ACHARJYA
2018-9-4
编辑:KALYAN ACHARJYA
2018-9-4
nEnergy(:,:) = (Energy(:,:) - min(Energy(:,:))) / (max(Energy(:,:)) - min(Energy(:,:)));
weight(:,:) = dot(nEnergy(:,:), delta(:,:));
5 个评论
Walter Roberson
2018-9-4
Energy is clearly a 2D array.
min(Energy) would be the same as min(Energy, [], 1), the minimum along the first dimension, which would return a row vector with the same number of columns as the second dimension.
(Energy(y,x) - min(Energy))
would therefore be a scalar left side minus a row vector right side. The result is well defined as being a row vector with the same number of columns as Energy has.
With Energy being a 2D array, (max(Energy) - min(Energy)) is a row vector with the same number of columns as Energy, minus a row vector with the same number of columns as Energy. The result is well defined as being a row vector wit hthe same number of columns as Energy has.
Now, your left hand side of the / is a row vector with the same number of columns as Energy, and your right hand side of the / is a row vector with the same number of columns as Energy.
The / operator is defined between two row vectors of the same number of columns. It is the least squared solution of an implied system of equations. V1 / V2 is equal to dot(V1, V2) / sum(V2.^2). This will be a scalar.
Are you certain that you want to be using / between two vectors to get the least squared solution like this ???
Stephen23
2018-9-4
编辑:Stephen23
2018-9-4
This answer uses the mrdivide / operator, which when applied to matrices (as this answer does), returns the least square solution of a system of equations. How is that in any way related to the original question? Answer: it isn't.
This gives a totally different output than the loops in the question. For a start, given Energy as a matrix (more than 1 column) then this answer returns nEnergy as just one single column, whereas the original code returns nEnergy with the same size as Energy.
I tried this on some random 3x4 matrices:
>> nE2 % this answer
nE2 =
0.71510
0.40187
0.40133
>> nE1 % the original loops
nE1 =
1.065124 0.646392 1.002962 -0.096295
0.412844 0.101908 0.535460 0.643355
-0.053346 1.175205 0.021929 0.469105
How does this actually solve the problem given in the question, given that it returns a different number of totally different values in a differently shaped array?
Note that because of how min operates along the first dimension of matrices, this answer cannot be "fixed" by changing to rdivide.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!