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

4 visualizaciones (últimos 30 días)
jaah navi
jaah navi el 16 de Jul. de 2021
Comentada: jaah navi el 17 de Jul. de 2021
I am having a cell array A in the folllowing manner
where
A=3x1 cell
2x3 double - [1,1,1 - 1st row
1,2,2]- 2nd row
2x3 double - [1,2,2 -1st row
1,1,1] -2nd row
2x3 double - [1,1,2 - 1st row
1,2,2] -2nd row
now, I want to convert A into B as given below
B=3x1 cell
2x1cell - [1,2,3] 1st row
[1] [2,3] 2nd row
2x2 cell -[1] [2,3] 1st row
[1,2,3] 2nd row
2x2 cell -[1,2] [3] 1st row
[1] [2,3] 2nd row
i.e.,
A=3x1 cell to B=3x1 cell
2x3 double - [1,1,1 to 2x1cell - [1,2,3]
1,2,2] to [1] [2,3]
2x3 double - [1,2,2 to 2x2 cell -[1] [2,3]
1,1,1] to [1,2,3]
2x3 double - [1,1,2 to 2x2 cell - [1,2] [3]
1,2,2] to [1] [2,3]
Could anyone please help me on this to do it on a general manner as my cell array size is larger.
  3 comentarios
Stephen23
Stephen23 el 17 de Jul. de 2021
jaah navi's incorrectly posted "Answer" moved here:
I want B to convert to A.
B=3x1 cell
2x3 double - [1,1,1 - 1st row
2x3 double - 1,2,2]- 2nd row
A=3x1 cell
2x1cell - [1,2,3] 1st row
2x2cell - [1] [2,3] 2nd row
Stephen23
Stephen23 el 17 de Jul. de 2021
jaah navi's incorrectly posted "Answer" moved here:
As mentioned
My
B is {} 1x1 cell
which contains
3x5 double as
1 1 2 3 4
1 2 3 3 4
1 2 2 3 4
Now I want to have
A as {} 1x1 cell
which contains
3x4 cell
as
[1,2] 3 4 5
1 2 [3,4] 5
1 [2,3] 4 5
I have also attached the mat file which I required for your reference

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 17 de Jul. de 2021
@jaah navi, why do you want to do this quirky thing anyway? What's the use case? Homework? Or is there some real world application?
This will do it:
s = load('required.mat')
Adesired = s.A
B = s.B
celldisp(Adesired)
celldisp(B)
% Find the size of the input array.
[rows, columns] = size(B{1})
% Make an output A that is the same size
% in case there are no repeated integers in B
A = cell(rows, columns)
% Scan B putting numbers into the right place in A
dblB = B{1};
for row = 1 : rows
thisRow = dblB(row, :);
for col = 1 : columns
if col > length(thisRow)
% thisRow got shortened so it no longer has as many columns as B.
continue;
end
vec = thisRow(col);
counter = 1;
while (col + counter) <= length(thisRow) && dblB(row, col) == thisRow(col + counter)
% Make a 2 element vector with the first number and the next different number.
index = min([length(thisRow), col+counter+1]); % Don't go past end of row!
vec = [thisRow(col) , thisRow(index)];
counter = counter + 1;
end
% If the vector has 2 elements, we need to shorten thisRow by one column.
if length(vec) >= 2
thisRow(col) = [];
end
A{row, col} = vec;
end
end
% Show in command window
A
% Crop off any columns in A that are all empty.
% Note: there might be a better way than a for loop but at least this is simple and understandable.
columnsToDelete = false(1, columns);
for col = columns : -1 : 1
deleteThisColumn = true; % Initialize.
for row = 1 : rows
if ~isempty(A{row, col})
deleteThisColumn = false; % Mark this column for keeping.
continue;
end
end
columnsToDelete(col) = deleteThisColumn; % Either true to delete, or false to keep.
end
% Do the deletion here, and show result in the command window.
A(:, columnsToDelete) = []
It shows:
A =
3×4 cell array
{[1 2]} {[ 2]} {[ 3]} {[4]}
{[ 1]} {[ 2]} {[3 4]} {[4]}
{[ 1]} {[2 3]} {[ 3]} {[4]}
  4 comentarios
Image Analyst
Image Analyst el 17 de Jul. de 2021
@jaah navi, again, why do you want to do this quirky thing anyway? What's the use case? Homework? Or is there some real world application?
After that, explain your recipe or algorithm for getting each cell, as apparently it's not clear to us. Explain it in words.
jaah navi
jaah navi el 17 de Jul. de 2021
The reason for converting B into A is I am implementing B in machine learning to train the model.
Once the model is trained I want to compute the throughput for B.
To compute the throughput I want B to convert into A as mentioned to get the code to execute the code to compute the throughput.
As for computing the throughput it requires A as the format i need to to the conversion.
I hope it clarifies. If not please let me know.

Iniciar sesión para comentar.

Categorías

Más información sobre Particle & Nuclear Physics 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