Remove empty rows from matrix
134 views (last 30 days)
Show older comments
Hi folks,
I have the following matrix:
0.6857 0.1248 0.9636 1.0884 0.4807 0.6077 0.3644 0 0 0
0.9093 1.0006 1.0006 1.0006 1.0006 0.3702 0 0 0 0
0.6367 1.0106 0.5669 0.4437 1.0106 1.0106 0.4608 0 0 0
0.2100 0.3250 1.1000 0.9750 0.1250 1.1000 0.4070 0 0 0
0.2048 0.4401 0.7116 1.1518 0.1521 0.2741 0 0 0 0
0.5575 0.2119 1.2212 0.3091 0.9121 0.8300 0.0993 0 0 0
0.6041 1.0239 0.6050 0.4189 1.0239 0.3428 0 0 0 0
0.5817 1.0205 0.4559 0.5645 1.0205 0.3417 0 0 0 0
0.9277 1.0209 0.5690 0.4519 1.0209 1.0209 0.4390 0 0 0
0.1817 1.0217 0.7974 0.2243 0.4393 0 0 0 0 0
0.4787 0.9174 0.2224 1.1398 0.7216 0.4182 0.3816 0 0 0
0.9091 1.0004 1.0004 1.0004 1.0004 0.5803 0 0 0 0
0.2126 1.0104 0.1849 1.1953 0.4451 0.2482 0 0 0 0
0.2531 0.2652 1.0657 1.0657 0.4962 0.5695 0.7034 0 0 0
0.2072 0.8844 0.2805 1.1650 0.5038 0.2651 0 0 0 0
0.3229 1.0774 0.0362 1.1136 1.1136 0.0092 0.3636 0 0 0
0.2172 1.2212 0.0671 1.1541 0.5882 0.2788 0 0 0 0
0.3084 1.0633 1.0633 0.3386 0.4706 0 0 0 0 0
0.2108 1.0037 1.0037 1.0037 0.3361 0 0 0 0 0
0.1521 0.0784 1.0078 1.0862 0.4669 0.6193 0.3637 0 0 0
0.9111 1.0026 1.0026 1.0026 1.0026 0.8622 0 0 0 0
0.1406 1.0046 1.0046 1.0046 1.0046 1.0046 1.0046 0.5075 0 0
0.1918 0.0487 1.0297 1.0784 0.5640 0.3634 0 0 0 0
0.1513 0.0229 1.2441 0.4140 0.8301 0.8509 0.0958 0 0 0
0.8749 1.0001 1.0001 0.0422 0 0 0 0 0 0
0.8900 1.0173 0.5089 0.5084 1.0173 1.0173 0.5565 0 0 0
0.8903 1.0177 0.7745 0 0 0 0 0 0 0
1.0498 0.0125 1.2351 0.4561 0.7790 0.4136 0 0 0 0
1.0150 0.7605 0.4198 0.8982 0 0 0 0 0 0
0.1808 1.0165 1.0165 1.0165 0.1931 0 0 0 0 0
0.8158 1.0071 0.2857 0.4807 0 0 0 0 0 0
0.8973 0.2251 0.9714 0.8502 0.3462 0.4006 0 0 0 0
0.2134 1.2000 0.1495 1.0506 0.7584 0.4417 0.3000 0 0 0
0.7503 1.0005 1.0005 1.0005 1.0005 0.5472 0 0 0 0
0.8519 1.1359 0.0001 0.8643 0 0 0 0 0 0
0.7315 0.4265 0.7546 0.3955 0 0 0 0 0 0
0.6675 0.8202 0 0 0 0 0 0 0 0
0.3814 1.0037 1.0037 1.0037 0.5490 0 0 0 0 0
0.3608 1.0934 0.0694 0.2967 0 0 0 0 0 0
0.1815 1.0204 1.0204 1.0204 1.0204 0.0804 0.6033 0 0 0
0.3523 1.0676 1.0676 0.0248 1.0429 0.5840 0 0 0 0
0.3721 0.6114 0.6717 0.4296 0 0 0 0 0 0
0.1866 1.0493 1.0493 0.5664 0.4830 1.0493 0.7450 0 0 0
0.2975 1.0260 0.0635 0.9624 1.0260 0.5612 0 0 0 0
0.3385 0.8884 0 0 0 0 0 0 0 0
0.2690 0.8727 0.3151 0.3977 0 0 0 0 0 0
0.2374 1.0482 0.7679 0.2802 1.0482 0.5733 0 0 0 0
0.1428 1.0986 0.2470 0.1208 0 0 0 0 0 0
0.1407 1.0821 1.0821 0.1822 0.8999 0.5919 0 0 0 0
0.1300 0.7610 0 0 0 0 0 0 0 0
0.9675 0.0053 0.4343 0 0 0 0 0 0 0
0.1817 1.0220 1.0220 1.0220 0.9418 0.0802 1.0220 0.2555 0 0
0.8054 1.0738 0.0971 0.9767 0.5874 0 0 0 0 0
0.7961 0.2463 0.5288 0 0 0 0 0 0 0
0.6895 0.6923 0.3524 1.0447 0.5715 0 0 0 0 0
0.6871 0 0 0 0 0 0 0 0 0
0.9155 0 0 0 0 0 0 0 0 0
0.4147 0 0 0 0 0 0 0 0 0
0.7308 0.0113 1.1596 0.8044 0.3522 0 0 0 0 0
0.6601 0.3453 0 0 0 0 0 0 0 0
0.5076 0.0782 0 0 0 0 0 0 0 0
0.7649 0.2744 0.5651 0 0 0 0 0 0 0
0.6115 0.3237 0.1214 0 0 0 0 0 0 0
0.7236 0.0776 0.6387 0 0 0 0 0 0 0
0.5311 0.1151 0.9963 0 0 0 0 0 0 0
0.1641 0.0599 0.9660 1.0259 0.5612 0 0 0 0 0
0.8965 0 0 0 0 0 0 0 0 0
0.6188 1.0043 0.1607 0 0 0 0 0 0 0
0.4533 0.3284 0.9263 0 0 0 0 0 0 0
0.3628 0.4568 0.4365 0 0 0 0 0 0 0
0.2558 0.5884 0 0 0 0 0 0 0 0
0.1884 0.4902 0 0 0 0 0 0 0 0
0.1455 0.0447 0.5526 0 0 0 0 0 0 0
0.0793 0.3838 0 0 0 0 0 0 0 0
0.0805 1.0149 0.6064 0 0 0 0 0 0 0
0.7523 0.8290 0.2982 1.1272 0.5694 0 0 0 0 0
0.1034 0.9373 0.3661 1.1931 0.1102 0.7427 0 0 0 0
0.0888 0.4431 0.6763 0.0638 0 0 0 0 0 0
0.0670 0.3839 0 0 0 0 0 0 0 0
0.0681 1.0164 0.6073 0 0 0 0 0 0 0
0.0755 0.4427 0.6843 0.0642 0 0 0 0 0 0
0.5619 0.4598 0.6869 1.1467 0.2045 0 0 0 0 0
0.4987 0.3906 0 0 0 0 0 0 0 0
0.5430 0.0114 1.0968 0.0632 0 0 0 0 0 0
0.4407 0.4027 0 0 0 0 0 0 0 0
0.4495 1.0963 0.2350 0.8613 0.5538 0 0 0 0 0
0.5038 0.4028 0 0 0 0 0 0 0 0
0.4943 0.0541 1.1424 0.0651 0 0 0 0 0 0
0.6954 0.4171 0 0 0 0 0 0 0 0
0.4677 0.2769 1.1634 0.0663 0 0 0 0 0 0
0.1589 1.1350 0.2493 0.8856 0.5733 0 0 0 0 0
0.3814 0.0163 1.2831 0.0731 0 0 0 0 0 0
0.6389 1.0369 0.4251 0 0 0 0 0 0 0
0.0710 1.0149 0.5551 0 0 0 0 0 0 0
0.4376 0.6881 1.0147 0 0 0 0 0 0 0
0.6393 1.1416 0.2036 0 0 0 0 0 0 0
0.3888 1.0800 0.1926 0 0 0 0 0 0 0
0.6852 0.3812 0 0 0 0 0 0 0 0
0.4217 0.7882 0.3831 0.6674 0 0 0 0 0 0
0.2469 1.0330 0.1550 0.2272 0 0 0 0 0 0
0.6003 0.1677 0.6004 0 0 0 0 0 0 0
0.3019 0.1893 1.0737 0.5634 0.0933 0 0 0 0 0
0.8692 0.1094 0.5962 0 0 0 0 0 0 0
0.3294 0.5862 0.7919 0.6614 0.0828 0 0 0 0 0
0.3616 1.1205 0.0095 0.5708 0 0 0 0 0 0
0.9761 0.2160 0.9891 0.4625 0 0 0 0 0 0
0.1902 1.0011 0.5057 0 0 0 0 0 0 0
0.2418 1.0338 1.0338 0.5295 0 0 0 0 0 0
0.2351 1.0051 0.1556 0.8495 0.4699 0 0 0 0 0
0.2517 1.0759 1.0233 0.0526 0.5968 0 0 0 0 0
0.2342 1.0014 1.0014 0.4238 0 0 0 0 0 0
0.2430 1.0389 0.9815 0.0574 0.4320 0 0 0 0 0
0.2707 0.5728 0.5845 1.1573 0.2449 0.2636 0 0 0 0
0.1065 0.1941 1.2852 0.1126 1.1726 0.4194 0.2113 0 0 0
0.5830 0.5932 0 0 0 0 0 0 0 0
0.4807 0.7002 0.5100 1.2102 0.0551 0.4094 0 0 0 0
0.8563 0.0122 0.5702 0 0 0 0 0 0 0
0.1487 0.9660 0.3856 0.3652 0 0 0 0 0 0
0.7246 0.1034 0.4980 0 0 0 0 0 0 0
0.2777 0.7889 0.3046 0 0 0 0 0 0 0
Columns 11 through 20
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
I want to remove all the rows which are all zeros.
Thanks
Accepted Answer
Star Strider
on 17 Sep 2018
If ‘M’ is your matrix, this works:
M = M(~all(M == 0, 2),:);
It will remove the ‘all zero’ rows using ‘logical indexing’.
4 Comments
Thiago de Aquino Costa Sousa
on 23 Nov 2022
And what if, instead of 0 I have empty [] cells??? How can I remove them?
Voss
on 23 Nov 2022
@Thiago de Aquino Costa Sousa: If you have a cell array (not a matrix as in the question) and you want to remove those rows whose elements all contain the empty array [], then you can do this:
% a cell array with some cells containing empty arrays []:
C = {1 [] 3; [] [] []; [7 8] [] []}
C = C(~all(cellfun(@isempty,C),2),:)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!