How to save certain values in a FOR loop

Hi, i am running several FOR-loops, as shown below, and want to save the step-values which satisfies the if-command. Right now I just stop the script at the first values which satisfies the criteria, but as there propably will be more than one answer, I would like to save all the different combinations in a matrix, or a file. How do I do this?
for k = 17.12:0.001:17.13
for c0 = 0.1:0.1:0.5
for c1 = 1:1:5
for ac = 0.1:0.1:0.5
for mc = 0.2:0.1:0.7
for a = 0:0.1:0.2
for m = 0:0.1:1
propellerdesign;
%This is the main script which runs as it should
if T_req<= T && T <= 1.001*T_req && cav_points == 0
return
end
end
end
end
end
end
end
end

 採用された回答

Elias Gule
Elias Gule 2016 年 4 月 1 日

0 投票

just before the start of the first for loop, define an empty matrix, this will be the storage for all the valid step-values. Replace the return statement with a statement that updates your storage matrix.
m = [];
for k = 17.12:0.001:17.13
for c0 = 0.1:0.1:0.5
for c1 = 1:1:5
for ac = 0.1:0.1:0.5
for mc = 0.2:0.1:0.7
for a = 0:0.1:0.2
for m = 0:0.1:1
propellerdesign;
%This is the main script which runs as it should
if T_req<= T && T <= 1.001*T_req && cav_points == 0
m = [m T_req]; % This causes your matrix to expand as necessary
end
end
end
end
end
end
end
end

3 件のコメント

Elias Gule
Elias Gule 2016 年 4 月 1 日
Alternatively, you can do the following:
valid_count = 0
m = nan*ones(10000,1) % A column vector of 10000 nans
%% Replace m = [m T_req] with the following lines
valid_count = valid_count + 1
m(valid_count) = T_req
%% After the loops, type the following
m = m(~isnan(m)) % Removes all the nan entries in from the matrix m
Anna
Anna 2016 年 4 月 3 日
Thank you so much! the second alternative worked out great :)
Manu K Sajan
Manu K Sajan 2019 年 9 月 30 日
Finallly!!!! after lots of googling, this method works perfectly!!!Thanks

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

その他の回答 (0 件)

カテゴリ

質問済み:

2016 年 4 月 1 日

コメント済み:

2019 年 9 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by