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

4 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by