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

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 Commenti

Where did the 3 (in B) come from in A? I don't see a 3 for any element in A.
Also, it's no problem to have a cell contain [1,2,3], but what does it mean to have a single cell contain [1] [2,3]? What kind of syntax is that? I don't believe it's MATLAB syntax. Do you mean [1,2,3} again? Like
ca{1} = [1,2,3]; % First row of call array ca.
ca{2} = [1,2,3]; % Second row of call array ca.
B{1} = ca; % Put the 2x1 cell array called ca into cell #1 of B.
Do something similar for B{2} and B{3}.
B{2} = ca; % Put the 2x1 cell array called ca into cell #2 of B.
B{3} = ca; % Put the 2x1 cell array called ca into cell #3 of B.
See the FAQ:
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
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

Accedi per commentare.

Risposte (1)

@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 Commenti

Ok. But what I actually need is if my input is B as mentioned in required.mat I need to have output as A in required.mat.
Just to show you how my output should appear I included both input B and output A in the same required.mat.
Could you please help me on this.
If my explanation is not clear please let me know.
Also, the output A should be displayed as
[1,2] 3 4 5
1 2 [3,4] 5
1 [2,3] 4 5
but when I run the code A is getting displayed as
[1,2] 2 3 4, here 2 3 4 should be as 3 4 5
1 2 [3,4] 4, here 4 should be as 5
1 [2,3] 3 4, here 3 4 should be as 4 5
@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.
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.

Accedi per commentare.

Categorie

Tag

Richiesto:

il 16 Lug 2021

Commentato:

il 17 Lug 2021

Community Treasure Hunt

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

Start Hunting!

Translated by