could anyone help me how to convert double to cell in an array.

6 次查看(过去 30 天)
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 个评论
dpb
dpb 2021-6-24
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?
jaah navi
jaah navi 2021-6-25
the pattern for the algorthm is as follows:
A has all rows in double that needs to be converted as cell as shown in each rows of B.
In specific each rows of A has three values for example i am considering the first row of A 1x3 double - [1,1,1] .
Here as all the three values are 1 (in double), it need to be converted to cell as [1,2,3]
For 1x3 double - [1,2,2] the first value is 1 and the second and third value is 2. So to convert, the first value that corresponds to 1 should be [1] and the remaining two values corresponds to 2 should be [2,3].
For 1x3 double -[1,1,2] the first two values are 1 and the third value is 2. So to convert, the value corresponds to first two values should be [1,2] and the third value corresponds to 2 should be [3].
If my explanation is still not clear please let me know.Also please help me on this.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2021-6-24
B=cell(size(A));
for i=1:numel(A)
B{i}=splitapply(@(x){x}, 1:numel(A{i}) ,A{i});
end
  3 个评论
Matt J
Matt J 2021-6-25
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
A{1}:
ans = 1×3
1 1 1
B{1}{1}:
ans = 1×3
1 2 3
A{2}:
ans = 1×3
1 2 2
B{2}{1}:
ans = 1
B{2}{2}:
ans = 1×2
2 3
A{3}:
ans = 1×3
1 1 2
B{3}{1}:
ans = 1×2
1 2
B{3}{2}:
ans = 3
jaah navi
jaah navi 2021-7-16
Could you please help me how to modify the above code
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
when A is [4x3 double; 4x3 double; 4x3 double] instead of [1x3 double; 1x3 double; 1x3 double]

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by