How do I create a matrix from another matrix excluding values?
4 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Karim
on 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])
% delete rows that have zero's in them
Example2(~any(Example2,2),:) = []
0 Comments
More Answers (1)
Torsten
on 11 Nov 2022
Edited: Torsten
on 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),:)
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!