How to vectorize this code to eliminate nested For loops?

조회 수: 1 (최근 30일)
Jae Min Lee
Jae Min Lee 2018년 9월 4일
편집: Stephen23 2018년 9월 4일
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
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 9월 4일
편집: Stephen23 2018년 9월 4일
Assuming that the first set of loops is supposed to normalize the data:
nEnergy = (Energy-min(Energy(:))) / (max(Energy(:))-min(Energy(:)));
w1 = delta.*nEnergy

추가 답변 (1개)

KALYAN ACHARJYA
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
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
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by