Generate multiple matrix variables using loop
Mostrar comentarios más antiguos
Hi,
I have the following code (just an extract, there are significantly more lines) I would like to use a loop to simplify its programming:
a1 = matrix(1).*vector + matrix
b1 = max(lambda.*a1, 0)
c1 = function.c(b1, matrix)
a2 = matrix(2).*vector + matrix
b2 = max(lambda.*a2, 0)
c2 = function.c(b2, matrix)
a3 = matrix3).*vector + matrix
b3 = max(lambda.*a3, 0)
c3 = function.c(b3, matrix)
Any suggestions would be greatly appreciated.
Thank you
2 comentarios
Stephen23
el 14 de Mzo. de 2022
"I would like to use a loop to simplify its programming"
The simple and efficient approach is to use arrays and indexing, just like MATLAB is designed for.
Your approach of numbering variable names will force you into writing slow, complex, inefficient code. Best avoided.
Respuestas (1)
Voss
el 14 de Mzo. de 2022
Here's an extract of an answer:
c = cell(size(matrix));
for ii = 1:numel(matrix)
a = matrix(ii).*vector + matrix;
b = max(lambda.*a, 0);
c{ii} = function_c(b, matrix);
end
Or doing the same thing more concisely:
c = cell(size(matrix));
for ii = 1:numel(matrix)
c{ii} = function_c(max(lambda.*(matrix(ii).*vector + matrix), 0), matrix);
end
Categorías
Más información sobre Loops and Conditional Statements 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!