Varying number of nested loops

2 ビュー (過去 30 日間)
ANOOP VK
ANOOP VK 2017 年 12 月 22 日
コメント済み: ANOOP VK 2017 年 12 月 26 日
I have the results of simulation in table format. Were the variables are the columns of the table. For example ,
Here I need to plot Histogram for Result for all the combinations of corner, voltage and temperature (two unique values for each hence eight combinations). In this case I can do it by three nested for loops . However the number of variables will depend upon the input data. That is , in another simulation I may have more columns added up. For example, one more column "Resistance" added up with two unique values (50,100). Now I have 2^4 = 16 combinations available. Hence four nested loops .
Is there any way to read these number of columns and generate that many nested loops? Or is there any other better way to do this.

採用された回答

Jos (10584)
Jos (10584) 2017 年 12 月 22 日
% small example, last column is result, first columns define combinations
DATA = [1 2 4 ; 1 3 15 ; 1 2 6 ; 1 2 8 ; 1 3 25]
[UniqueCombis, ~, IDX] = unique(DATA(:,1:size(DATA,2)-1), 'rows')
% now you can use IDX to pick from the last column of DATA
for k = 1:size(UniqueCombis,1)
tmpResult = DATA(IDX==k,end) ;
disp(sum(tmpResult))
end
% count how many times each combi is present in DATA
N = accumarray(IDX,1)
disp([UniqueCombis N(:)])
% etcetera ...
  1 件のコメント
ANOOP VK
ANOOP VK 2017 年 12 月 26 日
Hi. Could you please give more clarity on what is happening in this line [UniqueCombis, ~, IDX] = unique(DATA(:,1:size(DATA,2)-1), 'rows')
Thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by