Hello i want to write a for loop inside another for loop .
I drew this as an example to make it easier to understand.
My idea was to code something like this but it didnt work:
%%%%
for a = 1:200
for b = 1:7200
Calc(b,a) = (Array1(b) * Array2(b,a)) + (Array1(b-1) * Array2(b-1,a-1));
end
Table(b,a) = Calc(b,a) / constant;
end
%%%%
I would be very grateful if someone could help .
Thanks in advance.

댓글 수: 3

Julian
Julian 2023년 9월 7일
In your code is Calc, Table and Array of the dimension 7200x200 but in your picture 200x7200.
malik abdelli
malik abdelli 2023년 9월 7일
yes thank you i just corrected it
Julian
Julian 2023년 9월 7일
편집: Julian 2023년 9월 7일
But now your Array1 is also a 7200x200 matrix, then you would have to initialize that differently too.
Should you "only" pay attention to the image, or also to your code?
Please write down your problem mathematically. Then you basically have the answer in Matlab.

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

 채택된 답변

Julian
Julian 2023년 9월 7일
편집: Julian 2023년 9월 7일

0 개 추천

The size of your code and the size of the picture are different.
If you want the size of your code you have to change a and b in your matrices.
Calc = zeros(200,7200);
Table = zeros(200,7200);
for a = 2:size(Calc,1)
for b = 2:size(Calc,2)
Calc(a,b) = (Array1(b) * Array2(a,b)) + (Array1(b-1) * Array2(a-1,b-1));
end
Table(a,:) = Calc(a,:) / CONSTANT;
end
And you should try not to work with for-loops but with matrix operations. These are much faster in Matlab.
I don't know which dimensions are correct now, but for my code the quick variant should look like this:
Calc(2:end,2:end) = (Array1(2:end) .*Array2(2:end,2:end)) + (Array1(1:end-1) .*Array2(1:end-1,1:end-1));
Table = Calc/CONSTANT;

댓글 수: 3

malik abdelli
malik abdelli 2023년 9월 7일
편집: malik abdelli 2023년 9월 7일
yes but this still doesnt work they tell me that "Array indices must be positive integers or logical values."
I am assuming there is a problem with this " (Array1(b-1) * Array2(a-1,b-1))", since there is no value for Array1(0).
But i dont understand how i would do it otherwise
Julian
Julian 2023년 9월 7일
Oh, yes the loops have to start at 2 then, because otherwise you have 0 as index and in Matlab the indices start at 1.
malik abdelli
malik abdelli 2023년 9월 7일
ok thank you

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

추가 답변 (0개)

카테고리

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

제품

질문:

2023년 9월 7일

편집:

2023년 9월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by