Error using unique command

I have two values
A = [1000 16000 100000 30000 2000]
B = [150 100 4000 5000 2000];
I used code below to get the Sorted value of B according to A:
[ii jj k] = unique(A);
B = unique(B);
P = B(k);
C = [100 4000 2000 5000 150];
If my values of A are from -400 to 400 and and values of B are from -100 to 100 I get the error :
Index exceeds Dimension
Please provide assistance.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
편집: Azzi Abdelmalek 2012년 11월 24일

0 개 추천

Edited
A=[1000 16000 100000 30000 2000]
B=[150 100 4000 5000 2000];
[~,idx]=sort(A)
[~,idx1]=sort(idx)
C=B(idx1)

댓글 수: 21

Pat
Pat 2012년 11월 24일
But Azzi those sizes are same ,if not that code can you suggest some other code please
Pat
Pat 2012년 11월 24일
Azzi i am getting the same error
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
If they are the same length, you can't get an error, If they are not the same length you can't use P=B(jj), you can use P=A(jj)
Pat
Pat 2012년 11월 24일
Azzi same error
Index exceeds matrix dimensions.
Error in ==> sample at 64 P=B(jj)
if anything worng in that code,can you assist me with another code please
Pat
Pat 2012년 11월 24일
Anothet thing Azzi my both value size of A and B are same say 300x1
i used
g=unique(A);its size is 224x1
g1=uinque(B);its size is 87x1
Pat
Pat 2012년 11월 24일
size of A is 300x1
size of B is 300x1
Ok, have you changed your code orfer to
P=B(k) % then
B=unique(B);
Pat
Pat 2012년 11월 24일
I have done everthing what u have posted
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
Post your full code
Pat
Pat 2012년 11월 24일
YEs Azzi i get error
Index exceeds matrix dimensions.
Error in ==> sample at 64 P=B(k)
Pat
Pat 2012년 11월 24일
i changed even same error
Index exceeds matrix dimensions.
Error in ==> sample at 64 P=B(jj)
Pat
Pat 2012년 11월 24일
My code
A=csvread(''); 300x1
B=csvread(''); 300x1
Cw=A;
Sw=A;
for i=1:length(Cw)
if (Cw(i)<12 && Cw(i)>-12)
Cw(i)=0;
end
end
for i=1:length(Sw)
if (Sw(i)<12 && Sw(i)>-12)
Sw(i)=0;
end
end
A=Cw;
B=Sw;
if length(A)<length(B)
S=length(B)-length(A);
S1=zeros(S,1);
A=[A;S1];B;
elseif length(A)>length(B)
S=length(A)-length(B);
S1=zeros(S,1);
B=[B;S1];A;
else
A;B;
end
Cw=A;
Sw=B;
[r c]=size(Cw)
[r1 c1]=size(Sw)
Cw=double(Cw);Sw=double(Sw);
A=[];B=[];
A=Cw;B=Sw;
[ii jj k]=unique(A);
B=unique(B);
P=B(jj)
B=unique(B);
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
편집: Azzi Abdelmalek 2012년 11월 24일
Pat, remove the third line (counting from the last line) B=unique(B); You can even use B(k) if that's what you need
Pat
Pat 2012년 11월 24일
ok Azzi now it works ,the thing is the size of B differ which is B=unique(B); it is just 110x1 , after the same i should get same size ,why do i get different size,
Try this to understand
A=[1 1 1 2 2 12451];
[ii jj k]=unique(A)
check ii, jj and k
Pat
Pat 2012년 11월 24일
Thanks a lot Azzi ,i understand the concept ,even i perform B(k) the size varies,please provide a different code because that unique command is making the size less,in which i have problem in performing other steps
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
But I don't know what do you want to compute?
Pat
Pat 2012년 11월 24일
A=[1000 16000 100000 30000 2000]
B=[150 100 4000 5000 2000];
Sorting B according to A ,so i got output from above code as
C=[100 4000 2000 5000 150],minimum value of B comes under minimum value of A,,,,,,2nd minimum of B comes under 2nd minmum of A.........finally maximum of B comes under maximum of A
Pat
Pat 2012년 11월 24일
so finallu C will have same size of A and B
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
편집: Azzi Abdelmalek 2012년 11월 24일
or maby it's this
A=[1000 16000 100000 30000 2000]
B=[150 100 4000 5000 2000];
[~,idx]=sort(A)
[~,idx1]=sort(idx)
C=B(idx1)
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 24일
Pat, look at my edited answer

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

추가 답변 (2개)

Salil Goel
Salil Goel 2012년 11월 24일

0 개 추천

Are you making sure that the size of A and B is same when values in A are from -400 to 400 and B from -100 to 100? If not, then obviously you would get this error.
Matt Fig
Matt Fig 2012년 11월 24일
편집: Matt Fig 2012년 11월 24일

0 개 추천

% Sort B the same way A is sorted.
% We only need two calls to SORT.
[~,J] = sort(A);
BSA = sort(B);
J(J) = 1:length(A);
BSA = BSA(J) % This is B, sorted the same way A originally was.

댓글 수: 5

Pat
Pat 2012년 11월 29일
Thanks Fig,if there are unique and negatve values must be done
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 29일
Pat can you post an example
Pat
Pat 2012년 11월 29일
A=[120 150 12 10 36 25 ];
B=[2 9 1 6 8 10];
out=[9 10 2 1 8 6]
but no idea if it consists of negative and unique values
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 29일
What is the difference with the previous example?
Pat
Pat 2012년 11월 29일
No difference as u said i have posted an example..another problem am facing Azzi is that if i use
Ew1 = dec2bin(mod(2^16+Ew,2^16)),my all values gets converted to 1111111111111111
y i get all value as one,Ew constsist of values from -32768 to 32767

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

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

태그

질문:

Pat
2012년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by