Reshape a cell avoiding the loop

I have the cell for example:
a =
'0.0' '0.0'
'0.1' '-0.4'
'0.1' '0.8'
'0.2' '2.3'
'0.3' '3.4'
'0.3' '-3.2'
'0.4' '-6.9'
'0.5' '-1.8'
'0.5' '7.2'
'0.6' '10.0'
and I want o reshape it like this.
d =
'0.0' '0.0' '0.3' '-3.2'
'0.1' '-0.4' '0.4' '-6.9'
'0.1' '0.8' '0.5' '-1.8'
'0.2' '2.3' '0.5' '7.2'
'0.3' '3.4' '0.6' '10.0'
I want to avoid the loop because the cell dimensions are very large. Thank you!

 Réponse acceptée

Iain
Iain le 11 Juin 2013

0 votes

I think:
b = a';
c = reshape(b,cols,rows,numel(a)/cols/rows);
c = permute(c,[2 1 3]);
d = reshape(c,[],rows);
should do it...

6 commentaires

Giorgos Papakonstantinou
Giorgos Papakonstantinou le 11 Juin 2013
numel(a)/cols/rows will always be equal to 1.
cols and rows variables refer to b?
Iain
Iain le 11 Juin 2013
cols, is the number of columns in a, rows is the number of rows you want to segment by, so your updated example would have them both being two, and the third one dimension would be 5
So like this:
b = a';
c = reshape(b,2,2,numel(a)/2/2);
c = permute(c,[2 1 3]);
d = reshape(c,[],2);
but i get this:
d =
'0.0' '3.4'
'0.1' '-3.2'
'0.0' '0.4'
'-0.4' '0.5'
'0.1' '-6.9'
'0.2' '-1.8'
'0.8' '0.5'
'2.3' '0.6'
'0.3' '7.2'
'0.3' '10.0'
I think by accident you made a typo.
It should be:
reshape(c,2,[])
Thank you. I really don't understand how permute works.
Iain
Iain le 11 Juin 2013
Permute swaps dimensions round.

Connectez-vous pour commenter.

Plus de réponses (2)

the cyclist
the cyclist le 11 Juin 2013
Modifié(e) : the cyclist le 11 Juin 2013

0 votes

d = [a(1:5,:),a(6:10,:)];
You can generalize this, of course, but you did not provide enough detail. If you always want the top half and bottom half, for example, you could use the size() command to determine the "height", and use half the height to determine the cutoff, which I just hard-coded as 5 here.

1 commentaire

Giorgos Papakonstantinou
Giorgos Papakonstantinou le 11 Juin 2013
What if the the initial cell a has 100 rows and two columns (size 100x2). How do I cut the cell in sets of 5 avoiding the for loop. The resulting cell will have size 5x40 and it will start just as the cell d that I indicated above.

Connectez-vous pour commenter.

Giorgos Papakonstantinou
Giorgos Papakonstantinou le 11 Juin 2013

0 votes

I am sorry I should be more explanatory. Let's say that I want to split in sets of 2 and concatenate them.
so it would be:
c =
'0.0' '0.0' '0.1' '0.8' '0.3' '3.4' '0.4' '-6.9' '0.5' '7.2'
'0.1' '-0.4' '0.2' '2.3' '0.3' '-3.2' '0.5' '-1.8' '0.6' '10.0'

Community Treasure Hunt

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

Start Hunting!

Translated by