Asked by Saugata Bose
on 16 Jan 2019

Hi

If I have a single precision array of size 3x2, how could I convert it into a 3x1 cell array? as an example,

a=[ 1.1 1.2

2.1 2.2

3.1 3.2]

I would like to convert it as a cell array,b of size 3x1 of following :

b={{1x2 single} {1x2 single} {1x2 single}}

or b= { {1.1 1.2}

{2.1 2.2}

{3.1 3.2}

}

I have recieved suggestions from our 2 friends. But unfortunately these did not work for me. So I think, it may be my fault to explain the problem. For this reason, I have added 2 figures(these 2 are just an edited version of a data) to elaborate the problem:

After converting the single precision array the b should be appeared like this following figure:

The each cell contains the data inside like Figure 2:

Would you please suggest me the possible way of solution

thanks,

Answer by Stephen Cobeldick
on 16 Jan 2019

Edited by Stephen Cobeldick
on 16 Jan 2019

Simpler to use num2cell:

>> a = [1.1,1.2;2.1,2.2;3.1,3.2]

a =

1.1000 1.2000

2.1000 2.2000

3.1000 3.2000

>> b = num2cell(a,2);

>> b{:}

ans =

1.1000 1.2000

ans =

2.1000 2.2000

ans =

3.1000 3.2000

Saugata Bose
on 16 Jan 2019

hi stephen

Thank you very much. From this code, I learn to pplay with num2cell. But it did not solve my problem.

if u check the variables in the workspace you will see that it is providing 1X1 cell inside the 3x1 cell array whereas I am looking for 1X2 cell inside the 3x1 cell array.

Stephen Cobeldick
on 16 Jan 2019

In your question you asked for two different outputs:

b = {{1x2 single} {1x2 single} {1x2 single}}

or

b= { {1.1 1.2}

{2.1 2.2}

{3.1 3.2}

}

These outputs are not the same:

- the first output is a 1x3 cell array containing scalar cell arrays, each of which contains a 1x2 numeric,
- the second output is a 3x1 cell array containing 1x2 cell arrays, each cell of which contains a scalar numeric.

And because you wrote "or" you indicated that either of those outputs is acceptable. My previous comment gives you exactly the second output that you asked for. Perhaps you really only want the first output:

>> b = num2cell(num2cell(a,2));

>> b{:}

ans =

{[1.1000,1.2000]}

ans =

{[2.1000,2.2000]}

ans =

{[3.1000,3.2000]}

This is basicallywhat you asked for here:

b = {{1x2 single} {1x2 single} {1x2 single}}

except the b has size 3x1, not 1x3. You can easily transpose it if you want.

Jan
on 16 Jan 2019

+1. As far as I understand, either

a = single([1.1,1.2;2.1,2.2;3.1,3.2])

b = num2cell(num2cell(a), 2);

or explicitly

b = num2cell(num2cell(a, 2), 2);

solves the problem to get:

b={{1x2 single} {1x2 single} {1x2 single}}

Sign in to comment.

Answer by madhan ravi
on 16 Jan 2019

a=single([ 1.1 1.2

2.1 2.2

3.1 3.2]);

b=cell(size(a,1),1);

for i = 1:size(a,1)

b{i}=a(i,:);

end

b={b};

b{:}

Gives:

ans =

3×1 cell array

{1×2 single}

{1×2 single}

{1×2 single}

Saugata Bose
on 16 Jan 2019

Hi Madhan

Thanks for the suggestion. But it didnot help for me. So I have re edited my question. Could you please see the question again?

Thanks,

Sign in to comment.

Answer by Akira Agata
on 16 Jan 2019

Hi Saugata-san,

Thank you for providing more details. I believe the following will do the task you explained.

a = [1.1 1.2;

2.1 2.2;

3.1 3.2];

a = single(a);

b = mat2cell(a,[1 1 1]);

>> b

b =

3×1 cell array

{1×2 single}

{1×2 single}

{1×2 single}

Akira Agata
on 16 Jan 2019

Saugata Bose
on 16 Jan 2019

Dear Akira

As I am new to Matlab, may be for this reason, I am failing to clarify the problem. For this reason, I am using few pictures of the variables in workspace which I have been shown in the following figures:

The variable b should be displayed in the workspace as like this figure:

And the value of each cell should be displayed as like this figure:

Guillaume
on 16 Jan 2019

There is no point reposting the same information that you've put in the question. It wasn't clear the first time and it still isn't.

Please, read and answer the comments I've written to your question. Otherwise, you run the risk of getting people frustrated and giving up on helping you.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Akira Agata (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/439965-how-to-convert-to-a-cell-array-from-a-single-precision-array#comment_660879

## Saugata Bose (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/439965-how-to-convert-to-a-cell-array-from-a-single-precision-array#comment_660880

## Akira Agata (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/439965-how-to-convert-to-a-cell-array-from-a-single-precision-array#comment_660884

## Saugata Bose (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/439965-how-to-convert-to-a-cell-array-from-a-single-precision-array#comment_660897

## Guillaume (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/439965-how-to-convert-to-a-cell-array-from-a-single-precision-array#comment_661016

Sign in to comment.