How do I create a matrix from another matrix excluding values?
6 次查看(过去 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.
0 个评论
采纳的回答
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])
% delete rows that have zero's in them
Example2(~any(Example2,2),:) = []
0 个评论
更多回答(1 个)
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),:)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!