how to convert cell array to a matrix?

I have a 5X4 matrix whose each element is a cell array. how to convert it to a simple matrix of elements.

5 件のコメント

Bhowmik.U
Bhowmik.U 2018 年 7 月 30 日
try if this works
X=cell{5,4} %your cell Y=cell2mat(X) % your desired matrix
Stephen23
Stephen23 2018 年 7 月 30 日
@Zara Khan: what size would you expect the output to be? Would padding be acceptable to you? If so, with what value?
KSSV
KSSV 2018 年 7 月 30 日
Attach data instead of image snippet..have a look on Cell2mat
Zara Khan
Zara Khan 2018 年 7 月 30 日
I want to access this matrix as row by row
Zara Khan
Zara Khan 2018 年 7 月 31 日
Bhowmik U: No its not working as we cant use cell for cell arrays.

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

回答 (1 件)

Jos (10584)
Jos (10584) 2018 年 7 月 30 日

0 投票

A = {[1 2 3],[4 5],6 ; [11 12],13,[] ; 21, [22 23], [24 25]} % data
B = arrayfun(@(k) cat(2,A{k,:}),1:size(A,1),'un',0) % make row vectors
C = padcat(B{:}) % pad vectors with NaNs

6 件のコメント

Zara Khan
Zara Khan 2018 年 7 月 31 日
編集済み: Zara Khan 2018 年 7 月 31 日
its nice. But how to use this codes when I dont have some predefined values. Like I have a code:
d{i,j}=diff(A{i,j});
Now I want to make this d as simple matrix instead of cell arrays. How I will use your above codes in this case?
Jos (10584)
Jos (10584) 2018 年 7 月 31 日
use d instead of A to create B ... I am no sure if I understand your problem though ...
Zara Khan
Zara Khan 2018 年 7 月 31 日
How this will be when I am retrieving data from a loop ? If I use d instead of A then still I am using some predefined values.
Jos (10584)
Jos (10584) 2018 年 7 月 31 日
At some point you will have a cell array with values, won't you? Then you can transform it as above.
Zara Khan
Zara Khan 2018 年 7 月 31 日
yes that can be done using the above code. But what about when I am storing values from loops?
Jos (10584)
Jos (10584) 2018 年 8 月 1 日
This is all rather vague ... Can you show us some code, showing where you get stuck?

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

カテゴリ

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

質問済み:

2018 年 7 月 30 日

コメント済み:

2018 年 8 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by