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 件のコメント

Rik
Rik 2019 年 5 月 10 日
Will your vectors stay limited in length?
Akshay Kumar
Akshay Kumar 2019 年 5 月 10 日
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 2019 年 5 月 10 日
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?

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

 採用された回答

Jan
Jan 2019 年 5 月 10 日
編集済み: Jan 2019 年 5 月 10 日

1 投票

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 件のコメント

Akshay Kumar
Akshay Kumar 2019 年 5 月 15 日
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 2019 年 5 月 16 日
編集済み: Jan 2019 年 5 月 16 日
"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.
Akshay Kumar
Akshay Kumar 2019 年 5 月 16 日
Question is same,
Find the attached file,
Rik
Rik 2019 年 5 月 16 日
Don't use clear. There is usually no need to use it, because you should be using functions to keep your workspace clean.
Akshay Kumar
Akshay Kumar 2019 年 5 月 16 日
Thanks Rik but could you please see my problem and if possible please provide the solution.
Rik
Rik 2019 年 5 月 16 日
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 2019 年 5 月 16 日
@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 についてさらに検索

質問済み:

2019 年 5 月 10 日

コメント済み:

Jan
2019 年 5 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by