필터 지우기
필터 지우기

Select the column with at least one 1 in it

조회 수: 7 (최근 30일)
Rishi Balasubramanian
Rishi Balasubramanian 2020년 12월 20일
댓글: Walter Roberson 2020년 12월 20일
Hey people
Assume I have a m by n matrix of binary data. How would I be able to identify the column that has the least number of ones?
I used the sum function and compared the max and min. Later on in many loops I realised that it also identifies the column with zero 1s too. How do I avoid that?
The column to be selected must have a minimum of one 1s to n number of ones.
b = sum(H);
bmax = max(b);
bmin = min(b);
RC = find(b==min(b));
%This is what I am using which identifies the column with zero ones. Any simple methods?

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 20일
편집: Walter Roberson 2020년 12월 20일
find(any(H,1))
If you need at most n ones then:
b = sum(H,1);
find(b >= 1 & b <= n)
  댓글 수: 4
Rishi Balasubramanian
Rishi Balasubramanian 2020년 12월 20일
Consider a matrix H = 1x30.
The data in those elements are numbers 0, 1, 2, ......, n
I wanna be able select the column(s) with the values from 1.
If H has a '0', then columns having '1' must be returned.
If H has a '0', and has no 1s, then columns having '2' must be returned.
If H has a '0', and has no 1s and 2s, then columns having '3' must be returned.... and so on.
It should be able to return all the columns with a minimum value in it, where the minimum value cannot be zero. But it is any available minimum value after 0.
How do i achieve this?
Walter Roberson
Walter Roberson 2020년 12월 20일
any(H==min(H(H~=0)),1)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by