how to convert to a cell array from a single precision array
7 views (last 30 days)
Show older comments
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200804/pic%201.jpeg)
The each cell contains the data inside like Figure 2:
![[ic 2.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200805/[ic%202.jpeg)
Would you please suggest me the possible way of solution
thanks,
5 Comments
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?
Answers (3)
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
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:
- 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.
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}
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
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.
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!