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
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 24 日
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,
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 24 日
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 ExchangeShifting and Sorting Matrices についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by