Help with matrix problem

1 回表示 (過去 30 日間)
Andres  Romero
Andres Romero 2016 年 9 月 9 日
回答済み: Image Analyst 2016 年 9 月 9 日
I need to divide a matrix in 2, one matrix if has a '0' in column 4:
A = [10 15 17 14; 12 1 14 0;12 5 7 9;1 4 8 0;14 15 7 4]
B = [10 15 17 14;12 5 7 9]
C = [12 1 14 0;1 4 8 0]
Matrix B doesn't have '0' in column 4, and matrix C has '0' in column 4.

回答 (1 件)

Image Analyst
Image Analyst 2016 年 9 月 9 日
I'm not sure why the last row of A is not included in B. So the closest I can get to what you want it:
A = [10 15 17 14; 12 1 14 0;12 5 7 9;1 4 8 0;14 15 7 4]
% B = [10 15 17 14;12 5 7 9]
% C = [12 1 14 0;1 4 8 0]
rowsWithZeros = A(:, 4) == 0
B = A(~rowsWithZeros, :)
C = A(rowsWithZeros, :)
This B will have the last row of A since the last row of A does not have 0 in column 4. If you want to crop off that last row of A for some weird reason, you can do this:
B = A(~rowsWithZeros, :)
B = B(1:end-1, :); % Crop off last row of B.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by