can someone help me to write the code ?

Hi all
i'm trying to execute my program in matlab in order to ckeck results after a number of iteration under two variables B1 and B2, which are two row vectors with same dimension but i will focus only for the first element of each of them, it means i want to do a number of iteration in order to change the first element of each vector,
so i wrote my code as follow : here B1 should be started from 0 and B2 from 1000.
A_100_iteration = cell(51,1);
Result_100_iteration = cell(51,1);
for ib = 1:51
B1(1) = (ib-1)*20;
%code of the first variable
for ib1 = 50:-1 :1
B2(1) = (ib)*20;
%code of the first variable
for n = 1:100
%my program is here
A_100_iteration{ib}=[A_100_iteration{ib},A] ;
Result_100_iteration{ib}=[Result_100_iteration{ib},Result] ; %
end
B1_100_iteration{ib} = [B1_100_iteration{ib},B1];
B2_100_iteration{ib1} = [B2_100_iteration{ib1},B2];
end
end
the code doesn't work as i want, in each time the dimension of each vector has been changed,knowing that when i did same code for only B1 it works well.
any suggestion please

 採用された回答

Torsten
Torsten 2022 年 12 月 13 日

0 投票

A_100_iteration = cell(51,50,1);
Result_100_iteration = cell(51,50,1);
B1_100_iteration = cell(51,50,1);
B2_100_iteration = cell(51,50,1);
for ib = 1:51
B1(1) = (ib-1)*20;
%code of the first variable
for ib1 = 1:50
B2(1) = (51-ib1)*20;
%code of the first variable
for n = 1:100
%my program is here
A_100_iteration{ib,ib1}=[A_100_iteration{ib,ib1},A] ;
Result_100_iteration{ib,ib1}=[Result_100_iteration{ib,ib1},Result] ; %
end
B1_100_iteration{ib,ib1} = [B1_100_iteration{ib,ib1},B1];
B2_100_iteration{ib,ib1} = [B2_100_iteration{ib,ib1},B2];
end
end

8 件のコメント

Majid
Majid 2022 年 12 月 13 日
@Torsten thank you for your reply, i need just vectors for each A,Result, B1 and B2, with the modification you mentioned i get matrices for each one, when i remove "ib1" as following and i ran B2 also for 51 iterations, i get 51 rows for both B1 and B2, in each row i have the intial vector, but the problem each vector was repeated 51 times , it means instead of seeing the intial dimension of B1 and B2, i see that it was multiplied by 51.
so i'm asking when i put exactly the last two lines in my code in order to eliminate the repetition of the same vector.
A_100_iteration = cell(51,1);
Result_100_iteration = cell(51,1);
B1_100_iteration = cell(51,1);
B2_100_iteration = cell(51,1);
for ib = 1:51
B1(1) = (ib-1)*20;
%code of the first variable
for ib1 = 1:51
B2(1) = (51-ib1)*20;
%code of the first variable
for n = 1:100
%my program is here
A_100_iteration{ib}=[A_100_iteration{ib},A] ;
Result_100_iteration{ib}=[Result_100_iteration{ib},Result] ; %
end
B1_100_iteration{ib} = [B1_100_iteration{ib},B1];
B2_100_iteration{ib} = [B2_100_iteration{ib},B2];
end
end
Torsten
Torsten 2022 年 12 月 13 日
編集済み: Torsten 2022 年 12 月 13 日
B1(1) has range 1:51, B2(1) has range 1:50. For each of these combinations, you run your program. Thus you get 51*50 different results for A_100_iteration. To save these 51*50 results, you will need a matrix of size (51,50).
If you only want to combine B1(1) with B2(1) as value pairs (1/50),(2/49),(3/48), ... , you don't need a second loop. You can just set
for ib = 1:51
B1(1) = (ib-1)*20;
%code of the first variable
B2(1) = (51-ib)*20;
%code of the first variable
for n = 1:100
%my program is here
A_100_iteration{ib}=[A_100_iteration{ib},A] ;
Result_100_iteration{ib}=[Result_100_iteration{ib},Result] ; %
end
B1_100_iteration{ib} = [B1_100_iteration{ib},B1];
B2_100_iteration{ib} = [B2_100_iteration{ib},B2];
end
Majid
Majid 2022 年 12 月 13 日
@Torsten it works now .i really thank you very much for your help and effort usually contribute in order to give help! i appreciated, many thanks.
Majid
Majid 2022 年 12 月 14 日
Hi @Torsten , could you please help me to find the last element in the vector Result _100_iteration , because i have 51 vectors cell in each one the result of 100 iteration(it means 100 elements), but i want to see just the result for last iteration.
thanks in advance
Torsten
Torsten 2022 年 12 月 14 日
You mean the last cell, the last element in the last cell or the last element in each cell ?
Majid
Majid 2022 年 12 月 14 日
the last element in each cell
Torsten
Torsten 2022 年 12 月 14 日
編集済み: Torsten 2022 年 12 月 14 日
Still not motivated to take the online MATLAB course for free:
?
Only two hours - I think it would help you.
%Generate cell array of row vectors
A_100_iteration = cell(51,1);
for i = 1:51
A_100_iteration{i} = [2 3 4 5 6 7];
end
%Extract last element from each cell
Array_last_element = zeros(51,1);
for i = 1:51
Array_last_element(i) = A_100_iteration{i}(end);
end
Array_last_element
Array_last_element = 51×1
7 7 7 7 7 7 7 7 7 7
Majid
Majid 2022 年 12 月 14 日
okay , thank you very much

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2022 年 12 月 13 日

コメント済み:

2022 年 12 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by