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 Kommentare

Stephen23
Stephen23 am 14 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Voss
Voss am 14 Mär. 2022

0 Stimmen

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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 14 Mär. 2022

Kommentiert:

am 14 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by