suppose i have a column matrix a=( 2 2 2 3 3 3 5 5 5 4 4 3 3 3 2 2 3 3 3 4 4 4 5 5 5 4 4 3 3 )
i want to find the constant and increasing elements and put them separately in different columns of a matrix. eliminating the decreasing elements.
output(:,1) = ( 2 2 2 3 3 3 5 5 5 )
output(:,2) = (2 2 3 3 3 4 4 4 5 5 5 )

 採用された回答

Stephen23
Stephen23 2018 年 12 月 6 日
編集済み: Stephen23 2018 年 12 月 6 日

0 投票

a = [2;2;2;3;3;3;5;5;5;4;4;3;3;3;2;2;3;3;3;4;4;4;5;5;5;4;4;3;3]
idx = [true;diff(a)~=0];
idy = diff(a(idx))>0;
idd = diff([0;idy;0]);
idb = find(idd>0);
ide = find(idd<0);
ids = cumsum(idx);
fun = @(b,e)a(ismember(ids,b:e));
C = arrayfun(fun,idb,ide,'uni',0);
And checking the output:
>> C{:}
ans =
2
2
2
3
3
3
5
5
5
ans =
2
2
3
3
3
4
4
4
5
5
5

2 件のコメント

johnson saldanha
johnson saldanha 2018 年 12 月 6 日
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in accelerationrate (line 21)
idx = [true,diff(a)~=0];
im getting this error
Guillaume
Guillaume 2018 年 12 月 6 日
You're certainly not getting this error with the exact code that stephen gave. Of course, if your actual vector is a column vector instead of a row vector you either need to use vertical concatenation (i.e. use ; instead of , resulting in [true; diff(a)~=0]) or transpose your input into a row vector (i.e. [true, diff(a.')~=0]). First option is more efficient.

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

その他の回答 (1 件)

johnson saldanha
johnson saldanha 2018 年 12 月 6 日

0 投票

i did that but im not getting the desired output. im getting the decreasing values too
(5 5 5 5 3.75 3.75 3.75 3.75) im getting this in the output

10 件のコメント

johnson saldanha
johnson saldanha 2018 年 12 月 6 日
in one or two cells im getting only the increasing but majority of them im getting the decreasing values too
Stephen23
Stephen23 2018 年 12 月 6 日
@johnson saldanha: please upload some data that shows this behavior.
johnson saldanha
johnson saldanha 2018 年 12 月 6 日
i will upload the column vector.
johnson saldanha
johnson saldanha 2018 年 12 月 6 日
this is the output of one cell
Stephen23
Stephen23 2018 年 12 月 6 日
編集済み: Stephen23 2018 年 12 月 6 日
@johnson saldanha: please upload the input data in a .mat file (not in an .xlsx file) in a new comment (not in an answer).
johnson saldanha
johnson saldanha 2018 年 12 月 6 日
編集済み: johnson saldanha 2018 年 12 月 6 日
yes u can see theres a decrease from 13.75 to 12.5. i dont want the decrease here. the next set of increasing values should fall into the next cell. not the same cell
johnson saldanha
johnson saldanha 2018 年 12 月 6 日
csv file is okay ?
johnson saldanha
johnson saldanha 2018 年 12 月 6 日
the input
Stephen23
Stephen23 2018 年 12 月 6 日
編集済み: Stephen23 2018 年 12 月 6 日
See my edited answer, it should work now. For your .xlsx data it gives this:
>> C{:}
ans =
11.250
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
13.750
13.750
13.750
13.750
13.750
13.750
13.750
13.750
13.750
ans =
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
12.500
13.750
13.750
13.750
13.750
13.750
13.750
15.000
15.000
15.000
15.000
15.000
15.000
15.000
15.000
15.000
15.000
And for your .csv data it gives this:
>> C{:}
ans =
1.2500
1.2500
2.5000
3.7500
3.7500
3.7500
5.0000
5.0000
6.2500
6.2500
7.5000
7.5000
7.5000
8.7500
8.7500
8.7500
8.7500
8.7500
8.7500
8.7500
8.7500
8.7500
8.7500
8.7500
ans =
2.5000
2.5000
2.5000
2.5000
3.7500
3.7500
3.7500
3.7500
3.7500
5.0000
5.0000
5.0000
5.0000
5.0000
5.0000
5.0000
5.0000
5.0000
5.0000
ans =
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
3.7500
5.0000
5.0000
5.0000
5.0000
5.0000
5.0000
6.2500
6.2500
6.2500
6.2500
7.5000
7.5000
7.5000
7.5000
7.5000
8.7500
8.7500
8.7500
8.7500
8.7500
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
10.0000
ans =
8.7500
10.0000
10.0000
10.0000
10.0000
10.0000
11.2500
11.2500
11.2500
12.5000
12.5000
13.7500
13.7500
13.7500
15.0000
15.0000
15.0000
15.0000
16.2500
16.2500
16.2500
16.2500
16.2500
16.2500
17.5000
17.5000
17.5000
17.5000
17.5000
17.5000
18.7500
18.7500
18.7500
johnson saldanha
johnson saldanha 2018 年 12 月 6 日
thanks a lot. it works. and suppose i have to do the decrease they i have to change the > with < right

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by