How to compare two data sets based on maximum value

I have a data set that I split into thirds and I want to sort each row in the original data A based on the left and right maximum values. How can I compare these values so if the right value is greater than the left value, the row in the data set will invert. This is what I had so far.
[leftMAX,left_idx]=max(A(:,1:33),[],2);
[rightMAX, right_idx]=max(A(:,68:100),[],2);
[middleMAX, middle_idx]=max(A(:,34:67),[],2);
j= size(A,1);
for ii = 1:j
if leftMAX >= rightMAX
continue
elseif leftMAX <= rightMAX
b = fliplr(A(j,:));
A(j,:) = b;
end
end

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 10 日

0 投票

for ii = 1 : j
if rightMAX(ii) >= leftMAX(ii)
A(j, :) = A(j, end:-1:1);
end
end

3 件のコメント

Isha Punn
Isha Punn 2017 年 10 月 11 日
This did not flip the rows. rightMAX and leftMAX are 52x1 double and the overall data set is 52*100 double. I am trying to flip the rows based on the values for rightMAX and leftMAX for each row
Image Analyst
Image Analyst 2017 年 10 月 11 日
Please use save() to save A into a .mat file, then attach it.
Walter Roberson
Walter Roberson 2017 年 10 月 11 日
A = rand(52,100);
[leftMAX,left_idx]=max(A(:,1:33),[],2);
[rightMAX, right_idx]=max(A(:,68:100),[],2);
[middleMAX, middle_idx]=max(A(:,34:67),[],2);
saved_A = A; %only for cross-test
j = size(A,1);
for ii = 1 : j
if rightMAX(ii) >= leftMAX(ii)
A(ii, :) = A(ii, end:-1:1);
end
end
%cross test
for ii = 1 : j
if ~isequal(A(ii,:), saved_A(ii,:))
fprintf('Row %d not equal\n', ii);
end
end
... I originally used the wrong subscript in the assignment.

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

カテゴリ

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

質問済み:

2017 年 10 月 10 日

コメント済み:

2017 年 10 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by