Hello, Ineed help with this question. I have 2 vectors to compare. Matrix below is a simplified vector with 2 columns that I want to compare. I want to move across the columns from column 1 to column 2 to column 1 continously till the end of the vector in an ascending order. If I move across , I record a value +ve, if I move within the same column, I record -ve. E.g
2 to 3 is +ve
3 to 4 is +ve
4 to 5 is +ve
5 to 7 is -ve( same column)
7 to 9 is -ve of -ve = +ve (same column movement)
9 to 10 is +ve
10 t0 12 is +ve
12 to 16 is +ve
16 to 17 is -ve
17 to18 is +ve
18 to 19 is +ve
19 to20 is +ve

4 件のコメント

Dyuman Joshi
Dyuman Joshi 2022 年 11 月 9 日
You have skipped 12 to 15 and 15 to 16. Was that a mistake or intentional?
If it was intentional, what is the logic behind it?
Joshua Folorunso
Joshua Folorunso 2022 年 11 月 9 日
Hi @Dyuman Joshi, Thank you for the notice. It was a mistake
Rena Berman
Rena Berman 2022 年 12 月 27 日
(Answers Dev) Restored edit

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

 採用された回答

Matt J
Matt J 2022 年 11 月 9 日
編集済み: Matt J 2022 年 11 月 9 日

0 投票

I will assume here that the oddity mentioned above by @Dyuman Joshi is an error in the presentation of the example.
A=[2 3
4 5
10 7
15 9
16 12
17 18
19 20];
A=A';
B=A*0+[1;2];
[~,is]=sort(A(:));
B=2*abs(diff(B(is))')-1 %result
B = 1×13
1 1 1 -1 -1 1 1 1 -1 -1 1 1 1

7 件のコメント

Bruno Luong
Bruno Luong 2022 年 11 月 9 日
A variation without the need of B
A=[2 3
4 5
10 7
15 9
16 12
17 18
19 20];
[~,is] = sort(A(:));
Vesign = 2*abs(diff(is>size(A,1)))-1
Vesign = 13×1
1 1 1 -1 -1 1 1 1 -1 -1
Dyuman Joshi
Dyuman Joshi 2022 年 11 月 9 日
According to the statement provided above, expectec output should have 12 values, instead of 13 that you have gotten.
Bruno Luong
Bruno Luong 2022 年 11 月 9 日
編集済み: Bruno Luong 2022 年 11 月 9 日
You have 7 x 2 = 14 numbers, so 13 pairs of succesive numbers
2
3
4
5
7
9
10
12
15
16
17
18
19
20
@Dyuman Joshi has asked you miss the 12-15, 15-16 (you jump from 12-16, skipping 15) and you never answer why 15 is skipped.
Joshua Folorunso
Joshua Folorunso 2022 年 11 月 9 日
It was a mistake. Thank you.
Joshua Folorunso
Joshua Folorunso 2022 年 11 月 9 日
Thank you very much @Matt J. and @Bruno Luong. The answer is correct except that the 5th and 10th step should be positive because -ve of -ve gives +ve.
The 5 to 7 step gives -ve which is correct but 7 to 9 is -ve of previous -ve because the movement is still in the same column so it should be positive (+ve). The same applies to 16 to 17.
How can I easily correct this?
Thank you
Matt J
Matt J 2022 年 11 月 9 日
Perhaps as follows?
A=[2 3
4 5
10 7
15 9
16 12
17 18
19 20];
A=A';
B=A*0+[1;2];
[~,is]=sort(A(:));
B=cumprod(2*abs(diff(B(is))')-1) %result
B = 1×13
1 1 1 -1 1 1 1 1 -1 1 1 1 1
Joshua Folorunso
Joshua Folorunso 2022 年 11 月 9 日
Yes.
This is correct now. Thank you very much.

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

その他の回答 (2 件)

Bruno Luong
Bruno Luong 2022 年 11 月 9 日
編集済み: Bruno Luong 2022 年 11 月 9 日

0 投票

A=[2 3
4 5
10 7
15 9
16 12
17 18
19 20];
[As,is] = sort(A(:));
col = 1 + (is>size(A,1));
Vesign = 2*~~diff(col)-1;
Vesign = cumprod(Vesign);
Ves = string(Vesign);
Ves = repmat("+", size(Vesign));
Ves(Vesign<0) = "-";
S = As(1:end-1);
T = As(2:end);
col_S = col(1:end-1);
col_T = col(2:end);
T = table(S,T,col_S,col_T,Ves)
T = 13×5 table
S T col_S col_T Ves __ __ _____ _____ ___ 2 3 1 2 "+" 3 4 2 1 "+" 4 5 1 2 "+" 5 7 2 2 "-" 7 9 2 2 "+" 9 10 2 1 "+" 10 12 1 2 "+" 12 15 2 1 "+" 15 16 1 1 "-" 16 17 1 1 "+" 17 18 1 2 "+" 18 19 2 1 "+" 19 20 1 2 "+"

1 件のコメント

Joshua Folorunso
Joshua Folorunso 2022 年 11 月 9 日
Thank you very much. This is correct too.

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

Joshua Folorunso
Joshua Folorunso 2022 年 12 月 8 日

0 投票

I will.
It was a mistake while creating another question. I mistakenly edited old question.

カテゴリ

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

質問済み:

2022 年 11 月 9 日

コメント済み:

2022 年 12 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by