For loop iteration on each element of a matrix
Mostrar comentarios más antiguos
I have a n-dimension matrix as the variables of an equation. I want to iterate the equation solution on each element of the variable matrix. I know about using (for 1:numel(matrix)) but I want to store the solutions of the equation on the (ith,jth) element locations in a matrix.
Thanks
4 comentarios
Walter Roberson
el 20 de Ag. de 2020
Preallocate the output as the appropriate size. Then use linear indexing.
Or in some cases you can use arrayfun()
dpb
el 20 de Ag. de 2020
Or, this might be convenient place for a cell array--each cell holding the results of the solution, particularly if the solution is multidimensional.
More detail could lead to better answers...
Syed AWM
el 21 de Ag. de 2020
Bruno Luong
el 21 de Ag. de 2020
Editada: Bruno Luong
el 21 de Ag. de 2020
Other alternative, remove completely the for-loop
x1 = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
x2 = [4 3 2 1;5 7 6 8;9 12 11 11;16 13 15 14];
f1 = 4*x1.^2-x2.^3+28;
f2 = 3*x1.^3+4*x2.^2-145;
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!