Borrar filtros
Borrar filtros

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

6 visualizaciones (últimos 30 días)
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 comentarios
dpb
dpb el 24 de Jun. de 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 el 25 de Jun. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 24 de Jun. de 2021
B=cell(size(A));
for i=1:numel(A)
B{i}=splitapply(@(x){x}, 1:numel(A{i}) ,A{i});
end
  3 comentarios
Matt J
Matt J el 25 de Jun. de 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 el 16 de Jul. de 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]

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by