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 comentarios

Rik
Rik el 10 de Mayo de 2019
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.
Jan
Jan el 10 de Mayo de 2019
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?

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 10 de Mayo de 2019
Editada: Jan el 10 de Mayo de 2019
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 comentarios

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,
Jan
Jan el 16 de Mayo de 2019
Editada: Jan el 16 de Mayo de 2019
"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,
Rik
Rik el 16 de Mayo de 2019
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.
Rik
Rik el 16 de Mayo de 2019
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.
Jan
Jan el 16 de Mayo de 2019
@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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 10 de Mayo de 2019

Comentada:

Jan
el 16 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by