Generate multiple matrix variables using loop

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 Commenti

"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.

Accedi per commentare.

Risposte (1)

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

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Commentato:

il 14 Mar 2022

Community Treasure Hunt

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

Start Hunting!

Translated by