How can I arrange (collect) values for a couple of variables into a table outside "for loop" ?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have used "dataset" command to arrange the values of "B,R2,m,c" in a table like format; that is in a "for loop" which repeats 20 times. However, matlab shows the results independently per each trial as the following (I have pasted here only 4 trials just to understand it and not all the 20 trials);
ans =
B R2 m c
1 0.98463 0.35271 13.224
ans =
B R2 m c
2 0.96652 0.26335 12.687
ans =
B R2 m c
3 0.95112 0.21396 13.082
ans =
B R2 m c
4 0.93795 0.18173 13.783
My Question is: How can I tell Matlab to give me all the above values under one heading (I mean to look like a table format) as in the following (which I did it by my hand):
B R2 m c
1 0.98463 0.35271 13.224
2 0.96652 0.26335 12.687
3 0.95112 0.21396 13.082
4 0.93795 0.18173 13.783
댓글 수: 0
답변 (1개)
Jacob Halbrooks
2014년 3월 18일
This looks like a good use case for the TABLE function in MATLAB. You can create a table with your data variables and get a nice display of your data:
>> B = [1;2;3;4];
>> R2 = [0.98463; 0.96652; 0.95112; 0.93795];
>> m = [0.35271; 0.26335; 0.21396; 0.18173];
>> c = [13.224; 12.687; 13.082; 13.783];
>> t = table(B,R2,m,c)
t =
B R2 m c
_ _______ _______ ______
1 0.98463 0.35271 13.224
2 0.96652 0.26335 12.687
3 0.95112 0.21396 13.082
4 0.93795 0.18173 13.783
댓글 수: 2
Jacob Halbrooks
2014년 3월 19일
You can create an empty table before your FOR loop and then add a row of data in each iteration of the loop. For example, before the loop initialize the table with:
t = table([],[],[],[],'VariableNames', {'B','R2','m','c'});
Within your loop, your variables will get new values, which you can concatenate with t as shown below.
% Iteration 1
B = 1; R2 = 0.98463; m = 0.35271; c = 13.224;
t = [t; table(B, R2, m, c)];
% Iteration 2
B = 2; R2 = 0.96652; m = 0.26335; c = 12.687;
t = [t; table(B, R2, m, c)];
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!