Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
jaah navi
jaah navi am 24 Jun. 2021
Kommentiert: jaah navi am 16 Jul. 2021
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 Kommentare
dpb
dpb am 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?
jaah navi
jaah navi am 25 Jun. 2021
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 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 Kommentare
Matt J
Matt J am 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
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 am 16 Jul. 2021
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]

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by