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

5 ビュー (過去 30 日間)
jaah navi
jaah navi 2021 年 6 月 24 日
コメント済み: jaah navi 2021 年 7 月 16 日
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 Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by