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

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

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2022년 3월 14일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2022년 3월 14일

댓글:

2022년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by