Here is the thing
I have a large matrix and I need to sort that based on some columns:
B is the large matrix
A=sortrows(B,[1 4 9 10]);%Just a sample input vector
Its OK so far.But then in next step (I'm iterating this function) I will need this:
A=sortrows(B,[1 4 9 10 16]);%Notice I just add a new column to input vector
And same story in next iterations
So my question is how can I use previous sort results in each iteration?
Please consider that the sequence of input vector doesn't matter for me.And if it is possible please give me a note on this sortrows algorithm.
Edit------------ Can someone please explain sortrows algorithm?
Thanks in advance.

2 件のコメント

Image Analyst
Image Analyst 2013 年 8 月 19 日
Why do you even need to do the first sort? The second sort will give you the same results regardless of whether you had a sorted or unsorted array to begin with. So doing the first sort is useless. Is there some reason why you needed/wanted to do the first sort?
amir
amir 2013 年 8 月 19 日
Yes this is going to be a fitness function .I am evaluating the subset[1 4 9 10] in iteration t based on some fitness function and then in iteration t+1 I need to evaluate the subset[1 4 9 10 16] .In evaluation phase I am using sortrows as a preprocess then the rest.So if I can use sorted matrix in previous iteration it will save me god knows how much time!!

サインインしてコメントする。

 採用された回答

amir
amir 2013 年 8 月 19 日

0 投票

This is the function that i came up with might be useful for someone in future :
function [ndx]=sort2(x,cols,oldIndex)
x=x(:,cols);
[m,n] = size(x);
if nargin<3
ndx = (1:m)';
else
ndx=oldIndex;
end
for k = n:-1:1
[ignore,ind] = sort(x(ndx,k));
ndx = ndx(ind);
end
result--------
a=sort2(IS,[1 4 9 10]);
a=sort2(IS,16,a);
b=sort2(IS,[16 1 4 9 10]);
isequal(a,b)=1

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 19 日
編集済み: Azzi Abdelmalek 2013 年 8 月 19 日

0 投票

B=sortrows(B,[1 4 9 10])
B=sortrows(B,[1 4 9 10 16])

5 件のコメント

amir
amir 2013 年 8 月 19 日
Yes thanks but you didn't get the question I want to use first sort result in next one not repeat sorting all those columns.Its a matter of time efficiency.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 19 日
This is not obvious to find faster
amir
amir 2013 年 8 月 19 日
as far as i releaized about sortrows algorithm this two must have the same result :
%C is original matrix
A=sortrows(C,[16 1 4 9 10]);
B=sortrows(C,[1 4 9 10]);
B=sortrows(B,16);
But its not what is your opinion?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 19 日
In this case it's true, but it's not what you are asking. Check this case
C=randi(5,20) % Example
A=sortrows(C,[1 4 9 10 16]);
B=sortrows(C,[1 4 9 10]);
B=sortrows(B,16);
isequal(A,B)
A and B are different
amir
amir 2013 年 8 月 19 日
Please consider that sequence does not matter for me so [1 4 9 10 16] , [ 10 16 9 14] , ... any permutation is same thing.I just want to achieve a matrix sorted by any of these vectors based on a matrix that has already been sorted in last iteration based on [1 4 9 10] .I can not explain it better please excuse me for my poor english.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Shifting and Sorting Matrices についてさらに検索

質問済み:

2013 年 8 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by