From a cell to an array (besides cell2mat)

6 ビュー (過去 30 日間)
Nuno Palma
Nuno Palma 2016 年 12 月 15 日
コメント済み: Jos (10584) 2016 年 12 月 15 日
Let's say I have a cell called path, which consists of the following, for illustration purposes, [0] [1x2] [1x3].
Is there any way to transform it into a normal array, such that if I wanted to access, for example, path(1) = 0, path(2) = 1 2 and path(3) = 4 5 6 ( the values are just for illustration purposes).
Thanks for the help in advance.
  2 件のコメント
KSSV
KSSV 2016 年 12 月 15 日
Why not cell2mat? What problem you have with it?
Stephen23
Stephen23 2016 年 12 月 15 日
編集済み: Stephen23 2016 年 12 月 15 日
@Nuno Palma: do not call your variable path, because path is a very important function that will not work when you redefine its name. For the same reason never use the variable names size, length, cell, i, j, struct, etc.
"transform it into a normal array" What is a normal array? MATLAB does not have any data type named "normal". Do you mean a numeric array?
If you mean a numeric array then your examples
path(1) = 0, path(2) = 1 2 and path(3) = 4 5 6
make no sense because one element of a numeric array contains exactly one value, so it cannot have two, three, or more values. If you want one element to have multiple value then use a cell array.

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

採用された回答

Jos (10584)
Jos (10584) 2016 年 12 月 15 日
If all cells have a 1xN dimension, concatenation into a single row using comma-separate list expansion should work:
C = {[1] [2 3] [4 5 6]}
M = cat(2, C{:})
  2 件のコメント
Stephen23
Stephen23 2016 年 12 月 15 日
編集済み: Stephen23 2016 年 12 月 15 日
Actually this works for any MxX dimensioned arrays, where each array can have different X.
Jos (10584)
Jos (10584) 2016 年 12 月 15 日
Sure.

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

その他の回答 (1 件)

José-Luis
José-Luis 2016 年 12 月 15 日
編集済み: José-Luis 2016 年 12 月 15 日
C = {[1] [2 3] [4 5 6]}
result = [C{:}]

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by