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.

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 24 Nov 2012
Edited: Azzi Abdelmalek on 24 Nov 2012
Edited
A=[1000 16000 100000 30000 2000]
B=[150 100 4000 5000 2000];
[~,idx]=sort(A)
[~,idx1]=sort(idx)
C=B(idx1)

21 Comments

But Azzi those sizes are same ,if not that code can you suggest some other code please
Azzi i am getting the same error
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)
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
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
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);
I have done everthing what u have posted
Post your full code
YEs Azzi i get error
Index exceeds matrix dimensions.
Error in ==> sample at 64 P=B(k)
i changed even same error
Index exceeds matrix dimensions.
Error in ==> sample at 64 P=B(jj)
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 on 24 Nov 2012
Edited: Azzi Abdelmalek on 24 Nov 2012
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
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
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
But I don't know what do you want to compute?
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
so finallu C will have same size of A and B
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)
Pat, look at my edited answer

Sign in to comment.

More Answers (2)

Salil Goel
Salil Goel on 24 Nov 2012
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 on 24 Nov 2012
Edited: Matt Fig on 24 Nov 2012
% 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 Comments

Thanks Fig,if there are unique and negatve values must be done
Pat can you post an example
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
What is the difference with the previous example?
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

Sign in to comment.

Categories

Products

Tags

Community Treasure Hunt

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

Start Hunting!