# How can I create a matrix out of a matrix?

1 view (last 30 days)
Rengin on 17 Feb 2015
Edited: Mischa Kim on 19 Feb 2015
Hello,
% I have a matrix as below:
A=[0 0 0;5 2 4;0 0 0;4 1 8;9 5 6;0 0 0]
% My 2.,4. and 5. rows are different than '0'. As a result I wanna have a matrix as below including the number of rows different than '0':
B=[2 5 2 4;4 4 1 8;5 9 5 6];
However, I have a bigger matrix in my real problem. It is just an example
Thx

Elias Gule on 17 Feb 2015
indices = find(any(A,2)); B = [indices A(indices,:)];

Mat on 17 Feb 2015
B=sum(A,2)
C=find(B~=0)
D=[C A(C,:)]
??

Mischa Kim on 17 Feb 2015
Rengin, you could use, e.g.
B = [(1:size(A,1))' A];
B(~any(B(:,2:end),2),:) = []
Mischa Kim on 19 Feb 2015
Edited: Mischa Kim on 19 Feb 2015
B = [(1:size(Xs,1))' Xs];
B(~any(B(:,2:end),2),:) = []
to get
B =
1.0e+02 *
0.190000000000000 1.387778103306779 0.343814253001248
0.250000000000000 0.208481799046993 0.176385990188109
1.170000000000000 0.458209269814333 3.910781553669295
1.280000000000000 0.483323512292465 0.264376184324426
1.820000000000000 0.684481966455650 0.795152138548950
3.240000000000000 0.846839613465022 1.292539618758099
3.610000000000000 0.568404629169297 0.577528050340383
4.340000000000000 0.190023379731620 0.466716273193995
4.770000000000000 1.018698679681144 2.434206303256232
The first column contains the row numbers: 19, 25, 117, etc.
To your second question: the variable iter is set to 5 when you enter the for-loop, most likely because it is set to that value in one of the other scripts. Simply use a different (new) variable name, e.g. my_iter, and it'll work.