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

조회 수: 3 (최근 30일)
Karl
Karl 2022년 11월 11일
편집: Torsten 2022년 11월 11일
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.

채택된 답변

Karim
Karim 2022년 11월 11일
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

추가 답변 (1개)

Torsten
Torsten 2022년 11월 11일
편집: Torsten 2022년 11월 11일
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

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by