How can I create a matrix out of a matrix?

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

Respuestas (3)

Elias Gule
Elias Gule el 17 de Feb. de 2015

1 voto

indices = find(any(A,2)); B = [indices A(indices,:)];
Mischa Kim
Mischa Kim el 17 de Feb. de 2015
Rengin, you could use, e.g.
B = [(1:size(A,1))' A];
B(~any(B(:,2:end),2),:) = []

12 comentarios

Rengin
Rengin el 17 de Feb. de 2015
Hi Mischa,
When I use your code, I can add additional column in the matrix but it doesn't show the exact row numbers different than zero. If you consider my exapmle in the beginning, let's say it doesn't show the number of 2,4,5 in the first column. It is icreasing arithmetically e.g 1,2,3...
Not quite sure, I understand. When I run my code I get
B = [(1:size(A,1))' A];
B(~any(B(:,2:end),2),:) = []
B =
2 5 2 4
4 4 1 8
5 9 5 6
which is what the result is supposed to look like, according to your requirements. Am I missing something?
Rengin
Rengin el 17 de Feb. de 2015
Maybe I am doing sth wrong. In my matrix instead of '0' I have a penalty=1e5 actually. Can we modify the code accorging to that?
Mischa Kim
Mischa Kim el 17 de Feb. de 2015
Rengin, I assume your zeros (in matrix A) are not really zeros. Please post the entire code so we can re-produce.
Rengin
Rengin el 17 de Feb. de 2015
do u have e-mail address where I can send all my codes? cuz i have several m-files and in order to see results, you should carry out them all together.
Mischa Kim
Mischa Kim el 17 de Feb. de 2015
Please zip and attach to the comment. There's a paper clip icon on the right on top of the text box.
Rengin
Rengin el 17 de Feb. de 2015
I am working on loadflow_adjTAP.m. You'll see a matrix in 110th row named Xs.When you execute the script you'll get a 500x2 sized matrix. The most of the rows have penalty value. I wanna exract the values different than penalty with the rows. Those rows will be my optimal particles with optimal values.
Thx
Rengin
Rengin el 17 de Feb. de 2015
Cannot attach the file because I alreadt exceeded my daily limit. Is it possible for you to take a look tomorrow when I attach it?
Mischa Kim
Mischa Kim el 17 de Feb. de 2015
Will do.
Rengin
Rengin el 17 de Feb. de 2015
thx
Rengin
Rengin el 19 de Feb. de 2015
Hi Mischa,
I just wanted to make some regulations on my code and send it today. Actually I had another question, if you have time to take a look at my code. I create Ibestfirst matrix in 173th row and put it in two for loops. However, at the end, my matrix size changes to 5x500, although it has to be 1x500. I am not a programmer btw. I am just trying to write some basic codes to test some algorithms :(
thx
Mischa Kim
Mischa Kim el 19 de Feb. de 2015
Editada: Mischa Kim el 19 de Feb. de 2015
After computing Xs add
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Feb. de 2015

Editada:

el 19 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by