I need a code for sorting two column with a condition.

Suppose I have two column X and Y. Each column has same number of values (lets say 10). The condition is that if {X(i)-X(i+1)>0 and Y(i)-Y(i+1)>0} OR {X(i)-X(i+1)<0 and Y(i)-Y(i+1)<0} count it as one pair and take i = 1 for now. Total pairs which I want to check for this condition are 10C2 = 45pairs (X1 with X2,Y1 with Y2 after that X1 with X3,Y1 with Y3 and this will go on and will make 45 pairs)
Please help me out with this Problem
Thanks,

3 个评论

Will your vectors stay limited in length?
Yes.
you mean like for X and Y will number of values be same?
yes there will be equal number of values and they can vary like from 10 to 100 but in both column according to data.
What is the wanted output? You mention i=1, but what do you want to obtain finally? What have you tried so far and which problem do you have?

请先登录,再进行评论。

 采纳的回答

A simple approach with a loop:
% {X(i)-X(i+1)>0 and Y(i)-Y(i+1)>0} OR {X(i)-X(i+1)<0 and Y(i)-Y(i+1)<0}
n = 10;
X = rand(n, 1);
Y = rand(n, 1);
pairs = 0;
count = 0; % (remove this from productive code)
for d = 1:n-1
dx = X(1:n-d) - X(d+1:n);
dy = Y(1:n-d) - Y(d+1:n);
% Actually:
% pairs = pairs + sum((dx > 0 & dy > 0) | (dx < 0 & dy < 0));
% Easier:
pairs = pairs + sum(dx * dy > 0);
% Just to be sure (remove this from productive code):
count = count + numel(dx);
end
disp(count) % 45, as expected, (remove this from productive code)

7 个评论

Thanks Jan, Just want to clear my question again, could you please help me in this if i want to import my X and Y column values from matlab workspace.
please find attach file,
"import my X and Y column values from matlab workspace"
From which Matlab workspace? Each function has its own workspace. Usually you do not import data, but provide them as input arguments of a function:
% In the base workspace (the command window):
X = rand(1, 100);
Y = rand(1, 100);
yourFunction(X, Y);
% And in the file yourFunction.m:
function yourFunction(X, Y)
plot(X, Y);
end
I do not understand what you want me to do with the attached photo.
Question is same,
Find the attached file,
Don't use clear. There is usually no need to use it, because you should be using functions to keep your workspace clean.
Thanks Rik but could you please see my problem and if possible please provide the solution.
I did. The screenshot you posted showed your code contains a call to the clear function. That will remove X and Y from memory. You need to remove that call if you want any input to reach later code.
@Akshay Kumar: clear removes the existing variables. This is a brute method inside scripts to avoid to pollute the workspace. But it is smarter to use functions as I have explained already. But if you really want to use a function, omit the clear as Rik has explained.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 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!

Translated by