create cell array by extracting all the same row from multiple matrix

1 回表示 (過去 30 日間)
Katherine Zheng
Katherine Zheng 2022 年 9 月 28 日
コメント済み: Stephen23 2022 年 10 月 1 日
I have three double A = [105x3]; B = [105x3]; C = [105x3]; I want to create a cell array D = {105 cells} each cells contains the corresponding rows from A B C.
For example,
D {1} = [
A(1,:);
B(1,:);
C(1,:);]
each row from A B C are in different rows in D
Is this achievable through cellfun?

採用された回答

Stephen23
Stephen23 2022 年 9 月 28 日
"Is this achievable through cellfun?"
Yes:
A = rand(105,3);
B = rand(105,3);
C = rand(105,3);
D = cellfun(@vertcat,num2cell(A,2),num2cell(B,2),num2cell(C,2), 'uni',0)
D = 105×1 cell array
{3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double}
But most likely you should be using ND arrays rather than concatenating & splitting data like that.
  2 件のコメント
Katherine Zheng
Katherine Zheng 2022 年 9 月 28 日
Hi @Stephen23 thank you very much. That's exactally what I want. But I am not sure I understand the 'using ND arrays' in your suggestion. Could you possibly explain a bit? Many thanks.
Stephen23
Stephen23 2022 年 10 月 1 日
"But I am not sure I understand the 'using ND arrays' in your suggestion."

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

その他の回答 (1 件)

KSSV
KSSV 2022 年 9 月 28 日
編集済み: KSSV 2022 年 9 月 28 日
D = cell(105,5) ;
for i = 1:105
D{i} = [A(i,:) B(i,:) C(i,:)] ;
end
Or
D = [A B C] ;
D = num2cell(D,2) ;
  2 件のコメント
Katherine Zheng
Katherine Zheng 2022 年 9 月 28 日
Hi @KSSV, is it possible to achieve through cellfun?
KSSV
KSSV 2022 年 9 月 28 日
cellfun uses loop inside.....

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by