How to save an unknowm amount of matrixes? How to make boxplots of an unknown amount of matrixes?

조회 수: 1 (최근 30일)
% The script already functions but is there a kind of dynamic way to solve the problem? % The MatrixFGF5TBX3 is a valuesx2 Matrix % The Goal is to sort the Values of first row of the Matrix. The Point is, that the rows of the Matrix correspond to each other and are not allowed to be shuffled. The result should look like this:
0 21
0 2
0 1
1 0
1 32
% The next step is to create boxplots from the second column of the Matrix. But all in one graph! % I saved the sorted blocks in a cell but how can I run the boxplots then? % A Further point is that the MatrixFGF5TBX3 looks different each time - so with my approach I would have to check the script every time
wenneinmaldurch = 0;
g=0;
Max = max(MatrixFGF5TBX3(:,1));
FinalMatrix = zeros(1,2);
Matrixa = zeros(1,2);
Matrixb = zeros(1,2);
for g=0:Max
Matrixa = MatrixFGF5TBX3(MatrixFGF5TBX3(:,1)== wenneinmaldurch,:);
Matrixb = cat(1,FinalMatrix,Matrixa);
FinalMatrix = Matrixb;
wenneinmaldurch = wenneinmaldurch +1;
g = g+1;
end
wenneinmaldurch =0;
h=0;
k = 1;
TBX3Matrix = cell(Max,1);
for h =0:Max
TBX3Matrix{k} = FinalMatrix(FinalMatrix(:,1)==wenneinmaldurch,:);
wenneinmaldurch =wenneinmaldurch+1;
h=h+1; k = k+1;
end
figure;
x1 = TBX3Matrix{1}; x2 = TBX3Matrix{2}; x3 = TBX3Matrix{3};x4 = TBX3Matrix{4};x5 = TBX3Matrix{5}; x6 = TBX3Matrix{6};x7 = TBX3Matrix{7};x8 = TBX3Matrix{8};x9 = TBX3Matrix{9};x10 = TBX3Matrix{10};x11 = TBX3Matrix{11};x12 = TBX3Matrix{12};x13 = TBX3Matrix{13};x14 = TBX3Matrix{14};x15 = TBX3Matrix{15};x16 = TBX3Matrix{16};x17 = TBX3Matrix{17};x18 = TBX3Matrix{18};x19 = TBX3Matrix{19};x20 = TBX3Matrix{20};x21 = TBX3Matrix{21};x22 = TBX3Matrix{22};x23 = TBX3Matrix{23};x24 = TBX3Matrix{24};x25 = TBX3Matrix{25};x26 = TBX3Matrix{26};x27 = TBX3Matrix{27};x28 = TBX3Matrix{28};x29 = TBX3Matrix{29};x30 = TBX3Matrix{30};x31 = TBX3Matrix{31};x32 = TBX3Matrix{32};x33 = TBX3Matrix{33};x34 = TBX3Matrix{34};x35 = TBX3Matrix{35};x36 = TBX3Matrix{36};x37 = TBX3Matrix{37};x38 = TBX3Matrix{38};x39 = TBX3Matrix{39};x40 = TBX3Matrix{40};x41 = TBX3Matrix{41};
x1 = x1(:,2);x2 = x2(:,2);x3 = x3(:,2);x4 = x4(:,2);x5 = x5(:,2);x6 = x6(:,2);x7 = x7(:,2);x8 = x8(:,2);x9 = x9(:,2);x10 = x10(:,2);x11 = x11(:,2);x12 = x12(:,2);x13 = x13(:,2);x14 = x14(:,2);x15 = x15(:,2);x16 = x16(:,2);x17 = x17(:,2);x18 = x18(:,2);x19 = x19(:,2);x20 = x20(:,2);x21 = x21(:,2);x22 = x22(:,2);x23 = x23(:,2);x24 = x24(:,2);x25 = x25(:,2);x26 = x26(:,2);x27 = x27(:,2);x28 = x28(:,2);x29 = x29(:,2);x30 = x30(:,2);x31 = x31(:,2);x32 = x32(:,2);x33 = x33(:,2);x34 = x34(:,2);x35 = x35(:,2);x36 = x36(:,2);x37 = x37(:,2);x38 = x38(:,2);x39 = x39(:,2);x40 = x40(:,2);x41 = x41(:,2);
x = [x1;x2;x3;x4;x5;x6;x7;x8;x9;x10;x11;x12;x13;x14;x15;x16;x17;x18;x19;x20;x21;x22;x23;x24;x25;x26;x27;x28;x29;x30;x31;x32;x33;x34;x35;x36;x37;x38;x39;x40;x41];
g = [ones(size(x1));2*ones(size(x2));3*ones(size(x3));4*ones(size(x4));5*ones(size(x5));6*ones(size(x6));7*ones(size(x7));8*ones(size(x8));9*ones(size(x9));10*ones(size(x10));11*ones(size(x11));12*ones(size(x12));13*ones(size(x13));14*ones(size(x14));15*ones(size(x15));16*ones(size(x16));17*ones(size(x17));18*ones(size(x18));19*ones(size(x19));20*ones(size(x20));21*ones(size(x21));22*ones(size(x22));23*ones(size(x23));24*ones(size(x24));25*ones(size(x25));26*ones(size(x26));27*ones(size(x27));28*ones(size(x28));29*ones(size(x29));30*ones(size(x30));31*ones(size(x31));32*ones(size(x32));33*ones(size(x33));34*ones(size(x34));35*ones(size(x35));36*ones(size(x36));37*ones(size(x37));38*ones(size(x38));39*ones(size(x39));40*ones(size(x40));41*ones(size(x41));];
boxplot(x,g);
  댓글 수: 3
Philipp Fischer
Philipp Fischer 2016년 2월 25일
I know about that problem. A kind of that was my question I wanted to know about more. How can I ride there a loop?
Stephen23
Stephen23 2016년 2월 26일
편집: Stephen23 2016년 2월 26일
The functions cellfun and arrayfun get the values you want, without any loop (untested code):
C = cellfun(@(m)m(:,2),TBX3Matrix,'UniformOutput',false)
X = vertcat(C{:})
N = cellfun('size',TBX3Matrix,1);
D = arrayfun(@(n,x)x*ones(n,1),N(:)',1:numel(TBX3Matrix),'UniformOutput',false)
G = vertcat(D{:})
boxplot(X,G)

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2016년 2월 25일
편집: Stephen23 2019년 6월 19일
  댓글 수: 2
Philipp Fischer
Philipp Fischer 2016년 2월 25일
Thanks Stephen Cobeldick for your fast answer,
so you would suggest that my approach is for a beginner the best way?
And you would recommend in learning more about numeric arrays?
Thank you for your help.
Stephen23
Stephen23 2016년 2월 25일
편집: Stephen23 2016년 2월 25일
"you would suggest that my approach is for a beginner the best way"
No. For example you have written lots of numbered variables and copied-and-pasted code hundreds of times to create things like this:
x1 = x1(:,2);x2 = x2(:,2);x3 = x3(:,2);x4 = x4(:,2);x5 = x5(:,2);x6 = x6(:,2);x7 = x7(:,2);x8 = x8(:,2);x9 = x9(:,2);x10 = x10(:,2);x11 = x11(:,2);x12 = x12(:,2);x13 = x13(:,2);x14 = x14(:,2);x15 = x15(:,2);x16 = x16(:,2);x17 = x17(:,2);x18 = x18(:,2);x19 = x19(:,2);x20 = x20(:,2);x21 = x21(:,2);x22 = x22(:,2);x23 = x23(:,2);x24 = x24(:,2);x25 = x25(:,2);x26 = x26(:,2);x27 = x27(:,2);x28 = x28(:,2);x29 = x29(:,2);x30 = x30(:,2);x31 = x31(:,2);x32 = x32(:,2);x33 = x33(:,2);x34 = x34(:,2);x35 = x35(:,2);x36 = x36(:,2);x37 = x37(:,2);x38 = x38(:,2);x39 = x39(:,2);x40 = x40(:,2);x41 = x41(:,2);
There are much much simpler, faster, and neater ways of doing this. Computers are good at doing one thing: the same task, repeatedly. So why are you doing the computer's work? Just write a loop, or even better learn how to write vectorized code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by