Error using unique command

3 次查看(过去 30 天)
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
Edited
A=[1000 16000 100000 30000 2000]
B=[150 100 4000 5000 2000];
[~,idx]=sort(A)
[~,idx1]=sort(idx)
C=B(idx1)
  21 个评论
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
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
% 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 个评论
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by