フィルターのクリア

Concatenate arrays of different length into a matrix

375 ビュー (過去 30 日間)
Stefano Grillini
Stefano Grillini 2018 年 8 月 29 日
回答済み: Yang Liu 2024 年 1 月 26 日
Assume I have two arrays (time-series) of the form:
A = [NaN, 2, 3, 4, 5, 6, 7, NaN]
B = [5, NaN, 6, 7, NaN, 8, 9, 10, 11, 12]
Since two arrays of different length can not be horzcat (obviously), how can I combine them as to obtain a 8x2 matrix where available data match. I have long time-series, so this is just an example, but it points out how crucial it is to have matching observations. Ideally, the output should be:
C = [NaN, 2, 3, 4, 5, 6, 7, NaN; 5, NaN, 6, 7, NaN, 8, 9, 10]
Thanks
Stefano
  3 件のコメント
Stephen23
Stephen23 2018 年 8 月 29 日
編集済み: Stephen23 2018 年 8 月 29 日
"Since two arrays of different length can not be horzcat (obviously),"
I didn't have any problems using horzcat:
>> A = [NaN, 2, 3, 4, 5, 6, 7, NaN];
>> B = [5, NaN, 6, 7, NaN, 8, 9, 10, 11, 12];
>> horzcat(A,B)
ans =
NaN 2 3 4 5 6 7 NaN 5 NaN 6 7 NaN 8 9 10 11 12
Stefano Grillini
Stefano Grillini 2018 年 8 月 29 日
Apologise for the misunderstanding Stephen. The arrays are column vectors of the form
A = [NaN; 2; 3; 4; 5; 6; 7; NaN];
B = [5; NaN; 6; 7; NaN; 8; 9; 10; 11; 12];
Therefore dimensions are inconsistent for horzcat.
Thanks jonas. I'll have a look at synchronize(). By the way I need to run the MS_Regress_Fit function where the dependent variable is a matrix of two columns. My imported data are all vectors of size 187x1 with NaN. The problem is that MS_Regress_Fit does not accept NaN in the time-series. Therefore, before concatenating, I need to
milliq1(isnan(milliq1))=[];
lnavilliq(isnan(lnavilliq))=[];
But this command reduces the dimensions according to the number of NaNs so I'm unable to concatenate the two arrays.
Thanks
S

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

採用された回答

Stephen23
Stephen23 2018 年 8 月 29 日
編集済み: Stephen23 2018 年 8 月 29 日
Truncate to shortest length using indexing:
>> N = min(numel(A),numel(B));
>> [A(1:N);B(1:N)]
ans =
NaN 2 3 4 5 6 7 NaN
5 NaN 6 7 NaN 8 9 10
Pad to longest length using padcat:
>> padcat(A,B)
ans =
NaN 2 3 4 5 6 7 NaN NaN NaN
5 NaN 6 7 NaN 8 9 10 11 12
  5 件のコメント
Stephen23
Stephen23 2018 年 8 月 29 日
編集済み: Stephen23 2018 年 8 月 29 日
@Stefano Grillini: you really have two choices: either interpolate to fill in the NaN data, or remove the entire row from your data wherever there is a NaN. Judging by your data interpolation does not make much sense, however removing the rows is easy:
idx = any(isnan(c),2);
new = c(~idx,:)
Stefano Grillini
Stefano Grillini 2018 年 8 月 30 日
Thank you very much @Stephen! It's actually the only choice

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

その他の回答 (1 件)

Yang Liu
Yang Liu 2024 年 1 月 26 日
I'd say, the most straight forward method would be using cell to combine whatever dimension you have, and use Cell{a,b}(x,y) to access the elements.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by