How to vectorize this code to eliminate nested For loops?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jae Min Lee
el 4 de Sept. de 2018
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 comentario
Stephen23
el 4 de Sept. de 2018
Editada: Stephen23
el 4 de Sept. de 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?
Respuesta aceptada
Más respuestas (1)
KALYAN ACHARJYA
el 4 de Sept. de 2018
Editada: KALYAN ACHARJYA
el 4 de Sept. de 2018
nEnergy(:,:) = (Energy(:,:) - min(Energy(:,:))) / (max(Energy(:,:)) - min(Energy(:,:)));
weight(:,:) = dot(nEnergy(:,:), delta(:,:));
5 comentarios
Walter Roberson
el 4 de Sept. de 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
el 4 de Sept. de 2018
Editada: Stephen23
el 4 de Sept. de 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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!