Thread Subject: reshape

Subject: reshape

From: Jane

Date: 9 Mar, 2010 15:03:05

Message: 1 of 17

Hi,

I have a set of numbers 1002 x 20 :

1 2 3 4 5 6 7 8 9 10 11 12 ...n1002.
1 2 3
1 2 3
...n20

I want do the following.
~~~~~~~~~
A1 = kk(1,1:3:1002)
A2 = kk(1,2:3:1002)
A3 = kk(1,3:3:1002)

B1 = A1'
B2 = A2'
B3 = A3'
JJ(1:end,1:3) = [B1,B2,B3];
~~~~~~~~~

~~~~~~~~~
A4 = kk(2,1:3:1002)
A5 = kk(2,2:3:1002)
A6 = kk(2,3:3:1002)

B4 = A4'
B5 = A5'
B6 = A6'
JJ(1:end,4:6) = [B4,B5,B6];
~~~~~~~~~
.
.
.
continue until line 20.

How can I do this faster?

Subject: reshape

From: Oleg Komarov

Date: 9 Mar, 2010 17:55:21

Message: 2 of 17

"Jane " <Jane@mytrashmail.com> wrote in message <hn5nv9$n03$1@fred.mathworks.com>...
> Hi,
>
> I have a set of numbers 1002 x 20 :
>
> 1 2 3 4 5 6 7 8 9 10 11 12 ...n1002.
> 1 2 3
> 1 2 3
> ...n20
>
> I want do the following.
> ~~~~~~~~~
> A1 = kk(1,1:3:1002)
> A2 = kk(1,2:3:1002)
> A3 = kk(1,3:3:1002)
>
> B1 = A1'
> B2 = A2'
> B3 = A3'
> JJ(1:end,1:3) = [B1,B2,B3];
> ~~~~~~~~~
>
> ~~~~~~~~~
> A4 = kk(2,1:3:1002)
> A5 = kk(2,2:3:1002)
> A6 = kk(2,3:3:1002)
>
> B4 = A4'
> B5 = A5'
> B6 = A6'
> JJ(1:end,4:6) = [B4,B5,B6];
> ~~~~~~~~~
> .
> .
> .
> continue until line 20.
>
> How can I do this faster?

% Small example 4 by 12
kk = ndgrid(1:12,1:4).'
kk =
     1 2 3 4 5 6 7 8 9 10 11 12
     1 2 3 4 5 6 7 8 9 10 11 12
     1 2 3 4 5 6 7 8 9 10 11 12
     1 2 3 4 5 6 7 8 9 10 11 12

%{
You want to do:
A1 = kk(1,1:3:1002)
A2 = kk(1,2:3:1002)
A3 = kk(1,3:3:1002)

B1 = A1'
B2 = A2'
B3 = A3'
%}

% Which is equivalent to (look at the pattern):
[kk(1,1:3:end).',kk(1,2:3:end).',kk(1,3:3:end).']
ans =
     1 2 3
     4 5 6
     7 8 9
    10 11 12

% So if you actually have a matrix similar to kk from this example then
JJ = repmat(reshape(1:size(kk,2),3,[]).',1,size(kk,1))

% Else you have to be more precise...

Oleg

Subject: reshape

From: Jane

Date: 9 Mar, 2010 18:18:05

Message: 3 of 17

The values are not like this. They are x,y,z coordinates which looks like a nonuniform shape (has no pattern). It is like a grid. The rows are the different position of that grid in the depth. Another problem is how I can connect the xyz coordinates together to form a grid looking shape if the data is not structured in a way to join the points up to form a line. e.g. it would go from left to right and then back to the left again. I would want it to join from left to right and then up one then left then up one until it reaches the top and then come back down to form a grid. Maybe there is a plotting function that does that? Otherwise i would need to reshape the data plus add more data for it to join the points in a particular pattern using line.

Subject: reshape

From: Jan Simon

Date: 10 Mar, 2010 10:15:22

Message: 4 of 17

Dear Jane!

> I have a set of numbers 1002 x 20 :
> 1 2 3 4 5 6 7 8 9 10 11 12 ...n1002.
> 1 2 3
> 1 2 3
> ...n20
>
> I want do the following:
> A1 = kk(1,1:3:1002)
> A2 = kk(1,2:3:1002)
> A3 = kk(1,3:3:1002)
>
> B1 = A1'
> B2 = A2'
> B3 = A3'
> JJ(1:end,1:3) = [B1,B2,B3];
>
> ~~~~~~~~~
> A4 = kk(2,1:3:1002)
> A5 = kk(2,2:3:1002)
> A6 = kk(2,3:3:1002)
>
> B4 = A4'
> B5 = A5'
> B6 = A6'
> JJ(1:end,4:6) = [B4,B5,B6];
> ...

Did you pre-allocate JJ? (I assume so, otherwise "J(1:end, 1:3)" would fail).
It is faster to use JJ(:, 1:3) and omit the "1:end" in general.
Defining a pile of different arrays wastes a lot of memory. Better reuse the variables and write directly:
  B1 = kk(1, 1:3:1002)';
  B2 = kk(1, 2:3:1002)';
  B3 = kk(1, 3:3:1002)';
and in the next step:
  B1 = kk(2, 1:3:1002)';
  B2 = kk(2, 2:3:1002)';
  B3 = kk(2, 3:3:1002)';
etc.

Then you can cut and transpose the A<x> at once:
  A1 = transpose(kk(:, 1:3:1002));
  A2 = transpose(kk(:, 2:3:1002));
  A3 = transpose(kk(:, 3:3:1002));
  JJ = [A1(:, 1), A2(:, 1), A3(:, 1), A1(:, 2), A2(:, 2), A3(:, 2), ...]

But finally the task you perform is a reshaping and permutation of the array. So this must be possible with RESHAPE and PERMUTE directly:
  JJ = reshape(permute(reshape(transpose(kk), 3, 334, 3), [2, 1, 3]), 334, 9);

Kind regards, Jan

PS. It is helpful, if you provide working code instead of pseudo-code. I had to test the solution -of course I can can cofused by the permute&reshape stuff- and needed a working version of your program for comparison.

Subject: reshape

From: Jane

Date: 10 Mar, 2010 12:01:22

Message: 5 of 17

 for reshapexyz = 1:18
                C1(reshapexyz,1:30) = E1(reshapexyz,1:3:88);
                C2(reshapexyz,1:30) = E1(reshapexyz,2:3:89);
                C3(reshapexyz,1:30) = E1(reshapexyz,3:3:90);
 end
                C1 = C1'
                C2 = C2'
                C3 = C3'
             
             D = horzcat([C1(:,1),C2(:,1),C3(:,1)],[C1(:,2),C2(:,2),C3(:,2)],[C1(:,3),C2(:,3),C3(:,3)],
                 [C1(:,4),C2(:,4),C3(:,4)],[C1(:,5),C2(:,5),C3(:,5)],[C1(:,6),C2(:,6),C3(:,6)],[C1(:,7),C2(:,7),C3(:,7)],
                 [C1(:,8),C2(:,8),C3(:,8)],[C1(:,9),C2(:,9),C3(:,9)],[C1(:,10),C2(:,10),C3(:,10)],[C1(:,11),C2(:,11),C3(:,11)],
                 [C1(:,12),C2(:,12),C3(:,12)],[C1(:,13),C2(:,13),C3(:,13)],[C1(:,14),C2(:,14),C3(:,14)],[C1(:,15),C2(:,15),C3(:,15)],
                 [C1(:,16),C2(:,16),C3(:,16)],[C1(:,17),C2(:,17),C3(:,17)],[C1(:,18),C2(:,18),C3(:,18)]);

can you modify this for me? I am not sure if it is correct.

Subject: reshape

From: Jan Simon

Date: 10 Mar, 2010 12:47:04

Message: 6 of 17

Dear Jane!

> for reshapexyz = 1:18
> C1(reshapexyz,1:30) = E1(reshapexyz,1:3:88);
> C2(reshapexyz,1:30) = E1(reshapexyz,2:3:89);
> C3(reshapexyz,1:30) = E1(reshapexyz,3:3:90);
> end
> C1 = C1'
> C2 = C2'
> C3 = C3'
>
> D = horzcat([C1(:,1),C2(:,1),C3(:,1)],[C1(:,2),C2(:,2),C3(:,2)],[C1(:,3),C2(:,3),C3(:,3)],
> [C1(:,4),C2(:,4),C3(:,4)],[C1(:,5),C2(:,5),C3(:,5)],[C1(:,6),C2(:,6),C3(:,6)],[C1(:,7),C2(:,7),C3(:,7)],
> [C1(:,8),C2(:,8),C3(:,8)],[C1(:,9),C2(:,9),C3(:,9)],[C1(:,10),C2(:,10),C3(:,10)],[C1(:,11),C2(:,11),C3(:,11)],
> [C1(:,12),C2(:,12),C3(:,12)],[C1(:,13),C2(:,13),C3(:,13)],[C1(:,14),C2(:,14),C3(:,14)],[C1(:,15),C2(:,15),C3(:,15)],
> [C1(:,16),C2(:,16),C3(:,16)],[C1(:,17),C2(:,17),C3(:,17)],[C1(:,18),C2(:,18),C3(:,18)]);
>
> can you modify this for me? I am not sure if it is correct.

How could we know if this confusing piece of code is correct, if you do not tell, what D is expected to be ?!

Did my RESHAPE/PERMUTE method solved your problem?

Kind regards, Jan

Subject: reshape

From: Jane

Date: 10 Mar, 2010 13:34:02

Message: 7 of 17


The original array is 18x90.

I take from this data:
(1:18,1:3:90)
(1:18,2:3:90)
(1:18,3:3:90)

which equals the sizes
C1 =18x30
C2 =18x30
C3 =18x30

After transposing D is equal 30x54 as (1:3:90 = 30) and (3 * 18 = 54).

This means that 30 * 3 values are taken from row one of the original data and then transposed and then this becomes one set of data (consisting of 3 columns). Then next to those 3 columns there is another three columns which is equal to the second row down of the original data (after taking the 3 values from every column of the original data for that particular row).

I am not sure I understand nested permute, transpose etc.

Subject: reshape

From: Jan Simon

Date: 11 Mar, 2010 10:33:05

Message: 8 of 17

Dear Jane!

> The original array is 18x90.

At first you asked for a 1002 x 20 array. Are the questions connected?!

> I am not sure I understand nested permute, transpose etc.

Ok. Feel free to ask - this newsgroup is based on questions!

This solves your original question in a single line:
  JJ = reshape(permute(reshape(transpose(kk), 3, 334, 3), [2, 1, 3]), 334, 9);
And it is faster than the approach with its 20 subsections.
So how can this ugly line be made more understandable?
Split it into pieces:
  temp1 = transpose(kk);
  temp2 = reshape(temp1, 3, 334, 3);
  temp3 = permute(temp2, [2, 1, 3]);
  JJ = reshape(temp3, 334, 9);
Then go through this program line by line using the debugger and display the results in the command window. I've done this with a smaller input and used 1:numel(kk) as data, because I recognize integers more fluently.

Finally, at least in my tests, the JJ created by this line, equals your JJ. So actually the RESHAPE/PERMUTE does exactly what your program does, but it operates on the complete array at once. Therefore I assume it is much faster!

Kind regards, Jan

Subject: reshape

From: Jane

Date: 25 Mar, 2010 06:01:06

Message: 9 of 17


Hi Jan,

Sorry for the late reply. Your method above worked thank you. I understand what it is doing too. I have a new problem. If you could help that would be great.

I have an array which is 204020 X 3 double.

What I want to do is the following:

get values (1,1:3) & (405,1:3) & (809,1:3) & (1213,1:3) & (1617,1:3) & (2021,1:3)
(this is 404 increment)
transpose these and then

get values (2,1:3) & (406,1:3) & (810,1:3) & (1214,1:3) & (1618,1:3) & (2022,1:3)

transpose these and then (seperate to the above)

keep getting these values for example the first part would go from 1:404. the second from 405:808 the third from 809:1212 etc.

after getting the first line and transposing it shown above, get the second line transpose it and vertical concatinate it under the first keep going until the conditions are met above. (1:404) etc.
Then the end result is still a 204020 X 3 double.

Subject: reshape

From: Jan Simon

Date: 25 Mar, 2010 11:58:05

Message: 10 of 17

Dear Jane!

> Your method above worked thank you. I understand what it is doing too. I have a new problem.

If you've understood the effects of RESHAPE and PERMUTE, you have all you need to solve your new problem. Please try it.
For the beginning it is easier, if you test the method with smaller arrays. Then you can inspect the effetcs of the commands in the command line.

Kind regards, Jan

Subject: reshape

From: Jane

Date: 25 Mar, 2010 15:02:05

Message: 11 of 17

Hi,

I have tried. I am not sure how to pick rows.
For example the rows 1, 405, 809, 1213,1617,2021 are equal to 1x3 each so these 1x3 must be underneath each other.

maybe i need to do something like this but i am not sure.

A1=rand(30,3)
reshape(A1,1,30,3)
.
reshape to have.. the 1st , 6th , 11th, 16th, 21st, 26th, columns for all 3 dimensions in one dimension. Im not sure how to do this.

thank you.

Subject: reshape

From: Jan Simon

Date: 25 Mar, 2010 15:21:05

Message: 12 of 17

Dear Jane!

> I have tried. I am not sure how to pick rows.
> For example the rows 1, 405, 809, 1213,1617,2021 are equal to 1x3 each so these 1x3 must be underneath each other.
>
> maybe i need to do something like this but i am not sure.
>
> A1=rand(30,3)
> reshape(A1,1,30,3)
> .
> reshape to have.. the 1st , 6th , 11th, 16th, 21st, 26th, columns for all 3 dimensions in one dimension. Im not sure how to do this.

Please show us, what you have tried, and we can give some advices how to improve it!
E.g. try this:
  reshape(A1, 6, 5, 3)
and then:
  reshape(A1, 5, 6, 3)
Then:
  A2 = permute(reshape(5, 6, 3), [2, 1, 3])
  reshape(A2, 30, 3)
etc. I'm sure you will get familiar with RESHAPE and PERMUTE soon.

Kind regards, Jan

Subject: reshape

From: Jane

Date: 25 Mar, 2010 17:23:21

Message: 13 of 17


Hi I am still not sure how to use those functions well but I wrote this code for you. Matlab is going crazy and not ending.

countL=1;
j=(204020-403);
for z= 1:404
    for zz=z:404:j
        dd(countL,1:3)=KK(zz,1:3);
        countL=countL+1;
    end
    j=j+1;
end

Subject: reshape

From: Michael Hosea

Date: 25 Mar, 2010 18:10:47

Message: 14 of 17

Probably you haven't pre-allocated dd. Try adding

    dd = zeros(size(KK));

before the loop, or if KK can be any kind of array (e.g. char or an array of
structs or something)

    dd = KK;

is an easy way to initialize dd properly.

There are better ways to do this, but since I can't tell whether this is a
homework problem or not, I'll leave it at that.
--
Mike


"Jane " <Jane@mytrashmail.com> wrote in message
news:hog669$i92$1@fred.mathworks.com...
>
> Hi I am still not sure how to use those functions well but I wrote this
> code for you. Matlab is going crazy and not ending.
>
> countL=1;
> j=(204020-403);
> for z= 1:404
> for zz=z:404:j dd(countL,1:3)=KK(zz,1:3);
> countL=countL+1;
> end
> j=j+1;
> end

Subject: reshape

From: Jane

Date: 25 Mar, 2010 20:03:19

Message: 15 of 17


I figured the preallocation out already but I wanted to avoid using loops. I will keep the loop for now. Thank you

Subject: reshape

From: Michael Hosea

Date: 25 Mar, 2010 20:41:33

Message: 16 of 17

Jan Simon showed you the way in his March 11 post, i.e. use
reshape(permute(reshape(...
--
Mike

"Jane " <Jane@mytrashmail.com> wrote in message
news:hogfi7$1vj$1@fred.mathworks.com...
>
> I figured the preallocation out already but I wanted to avoid using loops.
> I will keep the loop for now. Thank you

Subject: reshape

From: Jane

Date: 25 Mar, 2010 22:44:03

Message: 17 of 17

Yes he did show me but that was a different problem which I solved march 11 and I find it hard to get used to these functions. Maybe there is a different way to solve my problem.

For example I have an array which is 15x3 double. This means 3d coordinate information. If I do the following:

plot3(A(1:15,1),A(1:15,2),A(1:15,3))

It would draw a line up and then go back down to the bottom and then up and then back down to the bottom. I wouldnt want the diagonal lines. So i could call the plot3 three times:

plot3(A(1:5,1),A(1:5,2),A(1:5,3))
plot3(A(6:10,1),A(6:10,2),A(6:10,3))
plot3(A(11:15,1),A(11:152),A(11:15,3))

but then I want to plot horizontally to form a grid. so I would need to specify the points directly? If there is an easier way to plot a grid on the screen please tell me because I have all of the points in the order specified above. I wanted to reshape the matrix so that I could plot horizontally with minimum specificaion in the plotting function. Thanks.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com