How to write for loop for an array calculation ?

I am trying to use below code to calculate and write into new variable but it's taking last value and giving same whole vector.
for k=1:3
for j = 22:24
%three values expected and write into A
A(k,:) = (f1_lh_nwaer(j,25)+f1_lh_nwaer(j+1,25))/2;
end
end
output:
A = [0.5;0.5;0.5]

5 件のコメント

Mathieu NOE
Mathieu NOE 2021 年 4 月 2 日
hello
sure , you are doing 3 times the same operation, so it is no surprise that A contains always the same value.
what is the intention ? why the 2 loops ?
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa 2021 年 4 月 2 日
I want to keep each calculation value of "j" into A. If you see
A(k,:) = (f1_lh_nwaer(j,25)+f1_lh_nwaer(j+1,25))/2;
% three results expected and write into A .
Sajid Afaque
Sajid Afaque 2021 年 4 月 2 日
if i understood correctly then,
it is because your inner loop runs over j several times for each k value
hence in A the k position which you have given is overwritter for each j.
count = 1;
for k=1:3
for j = 22:24
%three values expected and write into A
A(count,:) = (f1_lh_nwaer(j,25)+f1_lh_nwaer(j+1,25))/2;
count = count+1;
end
end
if this doesnt solves your query then please do explain your problem in other way as is it difficult to understand your current description of problem
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa 2021 年 4 月 2 日
It worked !
Thank you so much. I just don't need k=1:3 loop.
appreciated your help :)
Sajid Afaque
Sajid Afaque 2021 年 4 月 2 日
編集済み: Sajid Afaque 2021 年 4 月 2 日
Great that it worked. @Jeevan Kumar Bodaballa
if my comment helped you then please accept my answer, as my previous answer was in comment section

サインインしてコメントする。

 採用された回答

Sajid Afaque
Sajid Afaque 2021 年 4 月 2 日

0 投票

if i understood correctly then,
it is because your inner loop runs over j several times for each k value
hence in A the k position which you have given is overwritter for each j.
count = 1;
for k=1:3
for j = 22:24
%three values expected and write into A
A(count,:) = (f1_lh_nwaer(j,25)+f1_lh_nwaer(j+1,25))/2;
count = count+1;
end
end
if this doesnt solves your query then please do explain your problem in other way as is it difficult to understand your current description of problem

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by