How to vectorize this code to eliminate nested For loops?

1 view (last 30 days)
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 Comment
Stephen23
Stephen23 on 4 Sep 2018
Edited: Stephen23 on 4 Sep 2018
@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?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 Sep 2018
Edited: Stephen23 on 4 Sep 2018
Assuming that the first set of loops is supposed to normalize the data:
nEnergy = (Energy-min(Energy(:))) / (max(Energy(:))-min(Energy(:)));
w1 = delta.*nEnergy

More Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 4 Sep 2018
Edited: KALYAN ACHARJYA on 4 Sep 2018
nEnergy(:,:) = (Energy(:,:) - min(Energy(:,:))) / (max(Energy(:,:)) - min(Energy(:,:)));
weight(:,:) = dot(nEnergy(:,:), delta(:,:));
  5 Comments
Walter Roberson
Walter Roberson on 4 Sep 2018
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 on 4 Sep 2018
Edited: Stephen23 on 4 Sep 2018
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!