how to convert to a cell array from a single precision array

4 views (last 30 days)
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:
pic 1.jpg
The each cell contains the data inside like Figure 2:
[ic 2.jpg
Would you please suggest me the possible way of solution
thanks,
  5 Comments
Saugata Bose
Saugata Bose on 16 Jan 2019
Hi Akira
Would you please see my question again. I edited the question.
Guillaume
Guillaume on 16 Jan 2019
Edited: Guillaume on 16 Jan 2019
@Saugate,
It doesn't look like you're clear on exactly what you need. So, first make sure that you know that. Is it a cell array of cell array of scalars, a cell array of vectors, something else altogether? You've had plenty of answer giving you exactly what you asked (Stephen's answer is probably the best) and each time you come back with actually...
But first, I think you need to explain why you want to do that. You've good a perfectly useful array which is a lot easier to manipulate than a cell array and uses less memory. Why do you want to convert it into something else?

Sign in to comment.

Answers (3)

Stephen23
Stephen23 on 16 Jan 2019
Edited: Stephen23 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
  5 Comments
Stephen23
Stephen23 on 16 Jan 2019
Edited: Stephen23 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:
  1. the first output is a 1x3 cell array containing scalar cell arrays, each of which contains a 1x2 numeric,
  2. 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
Jan on 16 Jan 2019
Edited: 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.


madhan ravi
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}
  1 Comment
Saugata Bose
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.


Akira Agata
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}
  4 Comments
Saugata Bose
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:
pic2.jpg
And the value of each cell should be displayed as like this figure:
pic3.jpg
Guillaume
Guillaume on 16 Jan 2019
Edited: 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.

Community Treasure Hunt

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

Start Hunting!