フィルターのクリア

Is there a way to append all arrays nested in a table without looping?

13 ビュー (過去 30 日間)
Sam
Sam 2023 年 4 月 18 日
コメント済み: Stephen23 2023 年 9 月 12 日
Hello all,
I have a matlab table t with a column d of arrays as shown below. Is there a way to concatenate all the arrays in d to a new variable say D without looping through the array? I am avoiding looping because it is so slow.
Thanks and regards.
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 18 日
How do you want to concatenate them? Horizontally? Vertically? Or otherwise?
Stephen23
Stephen23 2023 年 9 月 12 日
The variable/column D must be a cell array inside that table, so you can simply use a comma-separated list:
For example, where T is your table:
D = horzcat(T.d{:})
D = vertcat(T.d{:})

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

回答 (2 件)

Rik
Rik 2023 年 4 月 18 日
Something like this should work:
t = table({19;17},{rand(100,1);rand(100,1)},'VariableNames',{'Umean','d'})
t = 2×2 table
Umean d ______ ______________ {[19]} {100×1 double} {[17]} {100×1 double}
D=cell2mat(t.d);
size(D),class(D)
ans = 1×2
200 1
ans = 'double'
  2 件のコメント
Sam
Sam 2023 年 4 月 18 日
Thanks guys. Now I have column of timetable where I only need that data. Is there a similar simple way to go about it?
Thanks in advance
Rik
Rik 2023 年 4 月 18 日
Can you post example code that generates a similar variable? I showed you an example, and as you can see, that way it is easy to show you what to do.
I expect cell2mat complains, since you must have already tried it. You can also use vertcat:
tmp = t.i;
D = vertcat(tmp{:});

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


KSSV
KSSV 2023 年 4 月 18 日
D = table2array(T.d)
  3 件のコメント
Stephen23
Stephen23 2023 年 4 月 18 日
編集済み: Stephen23 2023 年 4 月 18 日
"T.d directly results in vertical concatenation if the data is numeric (which it looks like is the case in OP's data)"
To get 100x1 numeric data in one row requires nesting the numeric data inside a cell array. In the Workspace Viewer this is displayed without the curly braces, exactly as the OP's screenshot shows.
Your example creates 1x100 numeric vectors in each row, not 100x1 vectors like the OP.
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 18 日
Stephen, I am aware that my example creates 1x100 numeric vectors, which is what I mentioned in my comment as well.
"In the Workspace Viewer this is displayed without the curly braces"
I overlooked this. I will delete my above comment.

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

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by