Assign double array to cell array

168 ビュー (過去 30 日間)
Douglas Anderson
Douglas Anderson 2021 年 11 月 12 日
コメント済み: Douglas Anderson 2021 年 11 月 12 日
Hello,
This seems like it should be obvious, but it isn't: I have several n x 1 cell arrays that I put into a table. Now I want to add the contents of an n x 1 double array, and it needs to be an n x 1 cell array.
Let's say n = 78. I've tried creating x = cell(78,1), but then can't seem to find a way to assign the the 78 x 1 double array y to x! Tried deal(), but that didn't work.
Missing something obvious, I am sure! Thanks.
Doug

採用された回答

Stephen23
Stephen23 2021 年 11 月 12 日
編集済み: Stephen23 2021 年 11 月 12 日
n = 78;
v = rand(n,1) % double array
v = 78×1
0.1725 0.6279 0.2847 0.9117 0.0171 0.1465 0.9297 0.5105 0.9125 0.3443
Either:
x = num2cell(v)
x = 78×1 cell array
{[0.1725]} {[0.6279]} {[0.2847]} {[0.9117]} {[0.0171]} {[0.1465]} {[0.9297]} {[0.5105]} {[0.9125]} {[0.3443]} {[0.5092]} {[0.0676]} {[0.2887]} {[0.1209]} {[0.5114]} {[0.4748]}
or:
x = cell(n,1);
x(:) = num2cell(v)
x = 78×1 cell array
{[0.1725]} {[0.6279]} {[0.2847]} {[0.9117]} {[0.0171]} {[0.1465]} {[0.9297]} {[0.5105]} {[0.9125]} {[0.3443]} {[0.5092]} {[0.0676]} {[0.2887]} {[0.1209]} {[0.5114]} {[0.4748]}
Storing numeric scalars in a cell array is inefficient and likely makes processing data harder.
  1 件のコメント
Douglas Anderson
Douglas Anderson 2021 年 11 月 12 日
Thank you!
I knew it had to be that simple, just made it harder for myself. Also, am working with output from someone else, so had to be appended to an existing cell array.

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

その他の回答 (1 件)

KSSV
KSSV 2021 年 11 月 12 日
A = rand(3)
B = mat2cell(A,size(A,1),ones(1,size(A,2)))
  1 件のコメント
Douglas Anderson
Douglas Anderson 2021 年 11 月 12 日
Thank you. Stephen's was even simpler.

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by