could anyone help me how to convert double to cell in an array.
4 views (last 30 days)
Show older comments
I am having a cell array A in the folllowing manner
where A=3x1 cell
1x3 double - [1,1,1]
1x3 double - [1,2,2]
1x3 double - [1,1,2]
now, I want to convert A into B as given below
B=3x1 cell
1x1cell - [1,2,3]
1x2 cell -[1] [2,3]
1x2 cell - [1,2] [3]
i.e.,
A=3x1 cell to B=3x1 cell
1x3 double - [1,1,1] to 1x1cell - [1,2,3]
1x3 double - [1,2,2] to 1x2 cell -[1] [2,3]
1x3 double - [1,1,2] to 1x2 cell - [1,2] [3]
Could anyone please help me on this to do it on a general manner as my cell array size is larger.
2 Comments
dpb
on 24 Jun 2021
To do anything in a "general" manner, there has to be some recognizable pattern that can be used as the basis for the algorithm -- what is the general rule here for an aribtrary size?
Accepted Answer
Matt J
on 24 Jun 2021
B=cell(size(A));
for i=1:numel(A)
B{i}=splitapply(@(x){x}, 1:numel(A{i}) ,A{i});
end
3 Comments
Matt J
on 25 Jun 2021
I don't understand what you say you are seeing, but we can easily add some lines as below to display the output. It matches what you say you want.
A=[1 1 1 ; 1 2 2; 1 1 2]; A=num2cell(A,2);
B=cell(size(A));
for i=1:numel(A)
B{i}=splitapply(@(x){x}, 1:numel(A{i}) ,A{i});
disp("A{" +i+ "}:"), A{i}
for j=1:numel(B{i})
disp("B{" +i+ "}{"+j+"}:"), B{i}{j}
end
disp ' '
end
More Answers (0)
See Also
Categories
Find more on Entering Commands 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!