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 commentaires

Stephen23
Stephen23 le 14 Mar 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.

Connectez-vous pour commenter.

Réponses (1)

Voss
Voss le 14 Mar 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

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by