MATLAB Answers

Organising cells from a cell array

6 ビュー (過去 30 日間)
Hari krishnan
Hari krishnan 2019 年 1 月 28 日
コメント済み: Hari krishnan 2019 年 1 月 28 日
Hi, i have a cell array 'a' which is of the form as shown below. I wanted to remove the empty cells from the cell array keep the elements corresponding to the row together one after another in 'b'. Can anyone help me to modify the code to get the 'desired_output' ? Any help will be appreciated
a =cell(1)
a{1,1} = 5
a{1,2} = 9
a{1,3} = 10
a{1,4} = 11
a{2,1} = 23
a{2,2} = 45
a{3,1} = 100
a{3,2} = 89
a{3,3} = 123
b = a(~cellfun('isempty',a))
'current_output'
b =
9×1 cell array
{[ 5]}
{[ 23]}
{[100]}
{[ 9]}
{[ 45]}
{[ 89]}
{[ 10]}
{[123]}
{[ 11]}
'desired_output '
d = d =
9×1 cell array
{[ 5]}
{[ 9]}
{[ 10]}
{[ 11]}
{[ 23]}
{[ 45]}
{[100]}
{[ 89]}
{[123]}

  0 件のコメント

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

採用された回答

Rik
Rik 2019 年 1 月 28 日
Matlab stores 2D arrays column by column, so if you want to keep it row by row, you need to transpose your array. The code below returns your desired output.
a =cell(1);
a{1,1} = 5;
a{1,2} = 9;
a{1,3} = 10;
a{1,4} = 11;
a{2,1} = 23;
a{2,2} = 45;
a{3,1} = 100;
a{3,2} = 89;
a{3,3} = 123;
a2=a';
b = a2(~cellfun('isempty',a2))

  1 件のコメント

Hari krishnan
Hari krishnan 2019 年 1 月 28 日
thankyou very much.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by