How do I create a matrix from another matrix excluding values?

5 Ansichten (letzte 30 Tage)
Karl
Karl am 11 Nov. 2022
Bearbeitet: Torsten am 11 Nov. 2022
Example = [ 5 0 2022 820 7 1 820;
5 1 2022 813 4 9 805;
5 2 2022 808 0 0 822;
5 3 2022 809 2 9 812;
5 4 2022 811 0 0 823;
6 5 2022 858 0 0 898;
6 6 2022 894 0 0 881;
6 7 2022 888 3 4 882;
6 8 2022 889 8 2 864;
6 9 2022 877 0 0 885]
Hello all, I am trying use this marix row 5 and 6 column values to create another matrix excluding the 0's to get:
Example2 = [7 1;
4 9;
2 9;
3 4;
8 2]
I am trying to achieve this using for loops and would appreciate any help.

Akzeptierte Antwort

Karim
Karim am 11 Nov. 2022
Hi, see below for a two step procedure to do this.
Example = [ 5 0 2022 820 7 1 820;
5 1 2022 813 4 9 805;
5 2 2022 808 0 0 822;
5 3 2022 809 2 9 812;
5 4 2022 811 0 0 823;
6 5 2022 858 0 0 898;
6 6 2022 894 0 0 881;
6 7 2022 888 3 4 882;
6 8 2022 889 8 2 864;
6 9 2022 877 0 0 885];
% copy selected columns to a new variable
Example2 = Example(:,[5 6])
Example2 = 10×2
7 1 4 9 0 0 2 9 0 0 0 0 0 0 3 4 8 2 0 0
% delete rows that have zero's in them
Example2(~any(Example2,2),:) = []
Example2 = 5×2
7 1 4 9 2 9 3 4 8 2

Weitere Antworten (1)

Torsten
Torsten am 11 Nov. 2022
Bearbeitet: Torsten am 11 Nov. 2022
Example = [ 5 0 2022 820 7 1 820;
5 1 2022 813 4 9 805;
5 2 2022 808 0 0 822;
5 3 2022 809 2 9 812;
5 4 2022 811 0 0 823;
6 5 2022 858 0 0 898;
6 6 2022 894 0 0 881;
6 7 2022 888 3 4 882;
6 8 2022 889 8 2 864;
6 9 2022 877 0 0 885];
Example2 = Example(:,5:6);
Example2 = Example2(any(Example2,2),:)
Example2 = 5×2
7 1 4 9 2 9 3 4 8 2

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by