Finding the most common element in the first row of a matrix

조회 수: 1 (최근 30일)
Josue Ortega
Josue Ortega 2022년 10월 24일
댓글: Bruno Luong 2022년 10월 24일
Hi there
I have a matrix A, and I am wanting to take the first non zero element of each column. For example, for
A =[
1 2 0 1,
4 1 2 2,
2 3 4 3,
3 4 1 4
]
I want to have
1 2 2 3
and then find the most common element.
I looked at previous answers, and used the following code
c=arrayfun(@(x)find(A(:,x),1,'first'),1:size(A,2));
The above code works, but when I use it in a loop of size more than 10 it does not work anymore, giving me an error. I wonder if there is cleaner syntax to use here. Thanks.
  댓글 수: 1
Bruno Luong
Bruno Luong 2022년 10월 24일
"The above code works"
Sorry it looks like an arbitrary code to me.

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

채택된 답변

Bruno Luong
Bruno Luong 2022년 10월 24일
Use mode command
[v,f]=mode([ 2 3 4 5 5 5 6 7 7])
v = 5
f = 3
  댓글 수: 3
Josue Ortega
Josue Ortega 2022년 10월 24일
Ah sorry I understand now why I wanted that. I do not want the most common value, I want the most common value of each column that is not zero.
A=[ 1 2 0 3;
2 0 3 4]
I want the answer to be 2.
Bruno Luong
Bruno Luong 2022년 10월 24일
I don't like moving question. I't a waste of time for me. I'll deltete my answer no point to comment further.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by