how to implementing energy function?

i want to implement this above energy function and i tried this following code, but i am getting 'Index exceeds matrix dimensions.' error. How can do this?
e=[];
V=WT.dec{1,1};
for x=1:17
for y=1:19
for z=1:18
e = e + (V(x,y,z))^2;
end
end
end

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 9 de Abr. de 2020
Editada: Ameer Hamza el 9 de Abr. de 2020
For loop is not needed. Use can use vectorized operation
Sum = sum(V.^2, 'all');

16 comentarios

Mehul Jain
Mehul Jain el 9 de Abr. de 2020
No not working getting error.
Ameer Hamza
Ameer Hamza el 9 de Abr. de 2020
What is the error? Please paste complete error message.
Which MATLAB release are you using?
Mehul Jain
Mehul Jain el 11 de Abr. de 2020
'Subscript indices must either be real positive integers or logicals.'
Mehul Jain
Mehul Jain el 11 de Abr. de 2020
R2016b
In MATLAB 2016b, change the line to
Sum = sum(V(:).^2);
Mehul Jain
Mehul Jain el 11 de Abr. de 2020
Same error.
what about
sum(sum(sum(V.^2)))
Ameer Hamza
Ameer Hamza el 11 de Abr. de 2020
Editada: Ameer Hamza el 11 de Abr. de 2020
make sure that no variable sum() is defined in your workspace. run
clear sum
before running the code.
Mehul Jain
Mehul Jain el 11 de Abr. de 2020
I am getting again same error.
I am not sure about the issue. There seem to be some issue with you MATLAB installation. What is the output of
which sum
Mehul Jain
Mehul Jain el 11 de Abr. de 2020
Ameer Hamza
Ameer Hamza el 11 de Abr. de 2020
Editada: Ameer Hamza el 11 de Abr. de 2020
Sorry, the correct command was
clear sum
not clear run, I have updated my comment. Try it again.
Mehul Jain
Mehul Jain el 11 de Abr. de 2020
Thank you so much it is working.
Mehul Jain
Mehul Jain el 11 de Abr. de 2020
I am having some confusion with the other two equations, will u plz help me out ???
Ameer Hamza
Ameer Hamza el 11 de Abr. de 2020
Editada: Ameer Hamza el 11 de Abr. de 2020
Please create a new question and paste the link in the comment below. I will try to help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 9 de Abr. de 2020

Comentada:

el 12 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by