Hi all,
I am trying to create a new array contains ONLY the unique values (same values but not repeated) which in the following case are 90 0 45
I used unique function, however, the function also returns the same data as in x, but in sorted order. I DO NOT want them in sorted order,
Also, I would like to obtain the index of the unique values in the x
like index=[1,2,9]
clear all;
clc;
x=[90 0 0 90 90 90 90 90 45 0 0 0];
y=unique(x)

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 12일
편집: Ameer Hamza 2020년 5월 13일

0 개 추천

clc;
x=[90 0 0 90 90 90 90 90 45 0 0 0];
[y, idx] = unique(x, 'stable')
output
y =
90 0 45
idx =
1
2
9

댓글 수: 4

@ Ameer Thanks, working perfectly,
just one more question, now the output sometimes, for the multidimensional array will be :
y(:,:,1)
y(:,:,2)
y(:,:,3)
y(:,:,4)
However, the two middle array are equal to zero , is it a way to only the output with values !!
clear all;
clc;
A(:,:,1) = [10;20];
A(:,:,2)=[1;2]
A(:,:,3)=[1;2]
A(:,:,4)=[100;200]
A(:,:,5)=[1;2]
for i=[1,4]
z(:,:,i)=A(:,:,i)
end
Correct syntax is i=1:4 (use colon)
for i=1:4
z(:,:,i)=A(:,:,i)
end
Ali Tawfik
Ali Tawfik 2020년 5월 13일
Hi,
Thanks for your prompt reply.
I understand, but I am looking for ONLY for index 1 and 4
So I wanna my output to be
z(:,:,1) = [10;20];
z(:,:,4)=[100;200]; which should be z(:,:,2)
Try try something like this
idx = [1 4];
for i=1:numel(idx)
z(:,:,i)=A(:,:,idx(i))
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2020년 5월 12일

댓글:

2020년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by