Cell to mat conversion

5 ビュー (過去 30 日間)
Chandan Prakash
Chandan Prakash 2021 年 2 月 24 日
コメント済み: Chandan Prakash 2021 年 2 月 24 日
Hi,
I have a cell array and need to convert to array.
a = {[100, 200, 300, 400, 500], [1000, 2000, 3000, 4000, 5000], [1100,1200, 1300, 1400, 1500]};
for each set b = a(1);
I need array data like,
b = [100, 200, 300, 400, 500];
i tired cell to mat, its not working.

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2021 年 2 月 24 日
If you want to extract the content of one cell simply do:
b = a{1};
If you want to merge a couple of cells into an array or matrix you can do:
B2 = cell2mat(a([1 3]));
B3 = cell2mat(a([1,3])');
HTH
  7 件のコメント
Bjorn Gustavsson
Bjorn Gustavsson 2021 年 2 月 24 日
You're welcome.
The reason was that you confused the two different indexings on cell-arrays, and then the data-format in the cell. When runing into these problems look at the data-type of the intermediate variables - that should reveal some of the reasons for the results or errors you get - for this I typically use (in your example):
whos b
That will tell the size and data-type of the variable b.
Chandan Prakash
Chandan Prakash 2021 年 2 月 24 日
OK, thank you for good explanation.

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

その他の回答 (1 件)

KSSV
KSSV 2021 年 2 月 24 日
編集済み: KSSV 2021 年 2 月 24 日
iwant = reshape(cell2mat(a),[],length(a))' ;
iwant(1,:)
  3 件のコメント
KSSV
KSSV 2021 年 2 月 24 日
a = {[100, 200, 300, 400, 500], [1000, 2000, 3000, 4000, 5000], [1100,1200, 1300, 1400, 1500]};
iwant = reshape(cell2mat(a),[],length(a))' ;
iwant(1,:)
ans = 1×5
100 200 300 400 500
Chandan Prakash
Chandan Prakash 2021 年 2 月 24 日
Hi,
Yeah i tired the way you suggested it works,
but for the data i have i'm getting result as char array in a single cell.
Thank you.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by