필터 지우기
필터 지우기

How to find unique values in a matrix without looping over rows

조회 수: 2 (최근 30일)
France Poirier
France Poirier 2021년 1월 27일
답변: Stephen23 2021년 1월 27일
Hi,
I have an m by n matrix A that has repeat non-zero values in every row. The number of non-zero elements varies across rows. I would like to keep only unique values, but can't seem to figure out how to do that without looping. Matrix size is big, so I tried looping and it's not an option...
I tried using unique function, but can't get it to do what I need. I would appreciate any advice.
Thank you,

답변 (2개)

Cris LaPierre
Cris LaPierre 2021년 1월 27일
out = unique(A(:))

Stephen23
Stephen23 2021년 1월 27일
M = randi([0,9],3,13)
M = 3×13
3 6 6 9 8 5 7 8 6 7 1 4 5 1 4 5 3 6 5 4 9 4 3 7 5 6 2 1 6 0 7 5 8 4 3 8 4 0 0
W = sort(M,2);
W(diff(W,1,2)==0) = 0;
W = sort(W,2) % optional
W = 3×13
0 0 0 0 0 1 3 4 5 6 7 8 9 0 0 0 0 0 0 1 3 4 5 6 7 9 0 0 0 0 0 1 2 3 4 5 6 7 8

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by