Sorting sub matrices based on specific column value
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello All, I try to optimize the following code. I have this example fib_dist_ang matrix. First column numbers are not relevant to discussion. second column values are sorted while third column are not sorted.
I wanted to sort the third column values. This sorting is based on same value in the second column and place the value closest to the theta_ran (e.g. theta_ran =7.0) value. During this sorting based on the third row, In this example, lets confine discussion to last three rows we have to change the all values in the row (like how SORTROWS() command does). Output I requested is as follows:
Notes: 1) I had used UNIQUE to find the first and last rows of the repetitions based on the second column value. 2) To find the closest value, I had used histc.
fib_dist_ang=%required output
2 0.008 5.2
22 0.008 6.2
42 0.014 4.7
43 0.016 7.2
3 0.016 2.2
23 0.016 6.7
fib_dist_ang= %input
22 0.008 6.2
2 0.008 5.2
42 0.014 4.7
23 0.016 6.7
43 0.016 7.2
3 0.016 2.2
%FINDING THE INDICES OF THE REPETATIONS
[~,uniq_first,~] = unique(fib_dist_ang(:,2), 'first');
[~,uniq_last,~] = unique(fib_dist_ang(:,2), 'last');
tic_sortang=tic;
for i2=1:size(uniq_first,1)
%SORTING BASED ON THE THRID COLUMN VALUES IN THE SUB MATRIX
ithrow=sortrows(fib_dist_ang(uniq_first(i2):uniq_last(i2),:),3);
%FINDING CLOSEST VALUE, IF AVAILABLE
val=theta_ran; f=ithrow(:,3);
if val > f(1) && val < f(end) % it can find the closest angle
[N,bin]=histc(val,f);%if val < f(1), then bin=0.
index=bin+1;
if abs(val-f(bin))<abs(val-f(bin+1))
fclosest=f(bin); index=bin;
else
fclosest=f(index);
end
% PLACING THE CLOSEST VALE TO THETA_RAN, IF AVALABLE
ithrow=[ithrow(index,:);ithrow(1:index-1,:);ithrow(index+1:end,:) ];
end
%COPY BACK THE SUB MATRIX INTO THE WHOLE MATRIX.
fib_dist_ang(uniq_first(i2):uniq_last(i2),:)=ithrow;
end
Only this loop is taking almost half of my total program time. Hence, I am looking for any possibility to optimize this program.
댓글 수: 4
Azzi Abdelmalek
2016년 2월 20일
Ok, the closest is value is 7.2, after 7.2 it's 6.7, why in your result, you placed 2.2 in the second position?
채택된 답변
Guillaume
2016년 2월 20일
I've not really tried to understand your code, nor have I run it, but if all you want to do is sort a column according to its distance to a certain value, then you simply need to sort according to abs(colvalue - referencevalue), so:
a = [22 0.008 6.2
2 0.008 5.2
42 0.014 4.7
23 0.016 6.7
43 0.016 7.2
3 0.016 2.2];
theta_ran = 7;
b = a;
b(:, 3) = abs(a(:, 3) - theta_ran);
[~, order] = sortrows(b, [2 3]); %sort first by 2nd column, then by distance from theta_ran in 3 rd column
a = a(order, :) %reorder a
추가 답변 (1개)
Azzi Abdelmalek
2016년 2월 20일
a=[22 0.008 6.2
2 0.008 5.2
42 0.014 4.7
23 0.016 6.7
43 0.016 7.2
3 0.016 2.2]
b=a;
b(:,4)=abs(b(:,3)-7)
[ii,jj,kk]=unique(a(:,2))
out=cell2mat(accumarray(kk,(1:numel(kk))',[],@(x) {sortrows(b(x,:),4)}))
out=out(:,1:3)
댓글 수: 7
Azzi Abdelmalek
2016년 2월 20일
편집: Azzi Abdelmalek
2016년 2월 20일
a=[22 0.008 6.2
2 0.008 5.2
42 0.014 4.7
23 0.016 6.7
43 0.016 7.2
3 0.016 2.2]
b=a;
[ii,jj,kk]=unique(a(:,2));
q=accumarray(kk,(1:numel(kk))',[],@(x) {sortrows(a(x,:),3)});
s=cell2mat(q);
[~,idx]=min(abs(s(:,3)-7));
n=cumsum(cellfun(@(x) size(x,1) ,q));
jdx=find(idx<=n,1);
r=sortrows([q{jdx} abs(q{jdx}(:,3)-7)],4);
q{jdx}=r(:,1:3);
out=cell2mat(q)
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!