MATLAB Answers

0

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

Asked by Saugata Bose on 16 Jan 2019
Latest activity Edited by Jan
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:
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

Seems strange. By running the following, b should be 3x1 cell array. I believe the same strategy can be applicable to yoru 1000X192 array.
a = [1.1 1.2;
2.1 2.2;
3.1 3.2];
b = mat2cell(a,[1 1 1]);
>> b
b =
3×1 cell array
{1×2 double}
{1×2 double}
{1×2 double}
Hi Akira
Would you please see my question again. I edited the question.
@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.

3 Answers

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

  5 Comments

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.
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.
+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}

  1 Comment

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}

  4 Comments

Show 1 older comment
Sorry, I don't understand what is a problem. In my try, b becomes a 3x1 cell array and each cell contains 1x2 array with single precision. I believe this output maches what you explained. So please tell us what is a difference, or what your desired output looks like.
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
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.