How to change an entire column.

5 ビュー (過去 30 日間)
Anton Mulder
Anton Mulder 2013 年 4 月 29 日
Hi,
I have a matrix with a dimension of 10x640. The matrix is filled with ones and zeros. If there is a zero in one of columns, I want the entire column to be zeros. How can I do this?
Example:
[[1,0,1];[1,1,1]]
Output should be:
[[1,0,1];[1,0,1]]

採用された回答

Iman Ansari
Iman Ansari 2013 年 4 月 29 日
A = randi(10,[10 640])-1;
B = A;
B(:,sum(A==0)>0)=0;
  1 件のコメント
Jan
Jan 2013 年 4 月 29 日
any(A, 2) is faster than sum(A==0)>0.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 4 月 29 日
Try this:
% Make sample data.
A = ones(10, 640);
% Stick a zero in columns 3, and 5.
A(9, 3) = 0;
A(8, 5) = 0
% Now that we have some sample data, do what Anton wants.
% Find out which columns have zeros in them
columnsWithZeros = any(~A); % Assumes A is only 0's and 1's.
A(:, columnsWithZeros) = 0; % Set whole column(s) to zero.
  1 件のコメント
Image Analyst
Image Analyst 2013 年 4 月 29 日
If you want a terse "one-liner" you can do this:
A(:, any(~A)) = 0;

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical (Boolean) Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by