Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!u16g2000pru.googlegroups.com!not-for-mail
From: Nathan <ngreco32@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Reshaping Dataset
Date: Fri, 26 Jun 2009 11:55:22 -0700 (PDT)
Organization: http://groups.google.com
Lines: 168
Message-ID: <565e5a43-680a-4524-a407-f329449f22e3@u16g2000pru.googlegroups.com>
References: <h20j9k$9ok$1@fred.mathworks.com> <h20s1i$evc$1@fred.mathworks.com> 
	<10ff833d-fad9-4a7a-8b86-2a46bca5ab85@y10g2000prc.googlegroups.com> 
	<h234eh$pms$1@fred.mathworks.com>
NNTP-Posting-Host: 198.206.219.34
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1246042522 6074 127.0.0.1 (26 Jun 2009 18:55:22 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 26 Jun 2009 18:55:22 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: u16g2000pru.googlegroups.com; posting-host=198.206.219.34; 
	posting-account=_KeVcAoAAAB7j3xn35ujaQ0BoQhuzwJP
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) 
	Gecko/2009060215 Firefox/3.0.11,gzip(gfe),gzip(gfe)
X-HTTP-Via: 1.1 wwwproxy-son-ca-02.ca.sandia.gov:80 (squid/2.5.STABLE14)
Xref: news.mathworks.com comp.soft-sys.matlab:550971


On Jun 26, 11:36 am, "Jeremy Bing" <jer...@mytrashmail.com> wrote:
> Nathan <ngrec...@gmail.com> wrote in message <10ff833d-fad9-4a7a-8b86-2a46bca5a...@y10g2000prc.googlegroups.com>...
> > On Jun 26, 10:55?am, "Jeremy Bing" <jer...@mytrashmail.com> wrote:
> > > "Jeremy Bing" <jer...@mytrashmail.com> wrote in message <h22vsa$qk...@fred.mathworks.com>...
> > > > Nathan <ngrec...@gmail.com> wrote in message <54b243f3-71a5-40cd-88b2-0b4b1efca...@l35g2000pra.googlegroups.com>...
> > > > > On Jun 26, 9:49?am, "Jeremy Bing" <jer...@mytrashmail.com> wrote:
> > > > > > "James Tursa" <aclassyguywithakno...@hotmail.com> wrote in message <h21081$dg...@fred.mathworks.com>...
> > > > > > > "James Tursa" <aclassyguywithakno...@hotmail.com> wrote in message <h20s1i$ev...@fred.mathworks.com>...
> > > > > > > > "Jeremy Bing" <jer...@mytrashmail.com> wrote in message <h20j9k$9o...@fred.mathworks.com>...
> > > > > > > > > Hi,
>
> > > > > > > > > I have data in the form of the following.
>
> > > > > > > > > X Y Z X Y Z ?>>
> > > > > > > > > X Y Z X Y Z
> > > > > > > > > X Y Z X Y Z
> > > > > > > > > X Y Z X Y Z
> > > > > > > > > ...
>
> > > > > > > > > But i need the data to be:
>
> > > > > > > > > X Y Z
> > > > > > > > > X Y Z
> > > > > > > > > X Y Z
>
> > > > > > > > > Do i used a case statement or a nested forloop to reshape the data? The dataset is quite large.
>
> > > > > > > > reshape(A',3,[])'
>
> > > > > > > > But this has a lot of data movement, the two transposes cause the entire dataset to be moved twice. If the time consumption is still too large then we could resort to a c-mex routine and limit the data movement to once, speeding things up.
>
> > > > > > > > James Tursa
>
> > > > > > > FYI, here is a bare-bones (no argument checking) c-mex file to do the same job. I get about a 20-30% speed improvement with this routine over the reshape method shown above. This may or may not be significant to you.
>
> > > > > > > James Tursa
>
> > > > > > > -----------------------------------
>
> > > > > > > #include "mex.h"
> > > > > > > void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
> > > > > > > {
> > > > > > > ? ? mwSize m, n, i, numel;
> > > > > > > ? ? double *target, *source1, *source2;
>
> > > > > > > ? ? m = mxGetM(prhs[0]);
> > > > > > > ? ? n = mxGetN(prhs[0]);
> > > > > > > ? ? numel = m * n / 2;
> > > > > > > ? ? plhs[0] = mxCreateDoubleMatrix(2*m, n/2, mxREAL);
> > > > > > > ? ? target = mxGetPr(plhs[0]);
> > > > > > > ? ? source1 = mxGetPr(prhs[0]);
> > > > > > > ? ? source2 = source1 + numel;
> > > > > > > ? ? for( i=0; i<numel; i++ ) {
> > > > > > > ? ? ? ? *target++ = *source1++;
> > > > > > > ? ? ? ? *target++ = *source2++;
> > > > > > > ? ? }
> > > > > > > }
>
> > > > > > Hi,
>
> > > > > > Thanks for the replies.
>
> > > > > > I'm not sure I understand the code. I have been trying to do it. My data has 63 columns and 294 rows however this depends on the file being input.
> > > > > > The format for each row is X Y Z X Y Z X Y Z X Y Z >>> 63. I need the all X, Y and Z data to be in one column rather in a row for every Row in the file. I.e I need every 3'd (3,6,9) column to be placed under column 1 (X). Same goes for Y (every second). Then when the first row is complete move to the second row and compile that.
>
> > > > > > Thanks.
>
> > > > > > Jeremy
>
> > > > > Have you tested any of this code? As far as I can tell, a there are a
> > > > > lot of ways to do what you are asking here (and it has been shown to
> > > > > be true).
> > > > > For example, the code I posted above works for your whole matrix.
> > > > > Tideman's code can be easily modified to make it work for your whole
> > > > > matrix.
> > > > > James Tursa's code (the one liner) will do the reshaping you are
> > > > > asking with minimal code (but as he said, it has a lot of data
> > > > > movement which can be a problem with larger matrices).
> > > > > If you are used to MEX files, you can compile his C code as a MEX file
> > > > > and run it through that (and I'm assuming it will STILL do exactly
> > > > > what you asked for).
>
> > > > > Why not try these things out and see which one you like best for your
> > > > > program?
> > > > > If you don't understand the code, just ask a specific question about
> > > > > specific code and I'm sure we will be more than willing to let you
> > > > > know what it does.
>
> > > > > -Nathan
>
> > > > Ok I will try again.
>
> > > This seems to work but how do i concatenate the variable B1 for the loop. (store the data).
>
> > > ? ? r = cell2mat(r);
> > > ? ? rnum = numel(r(:,1));
> > > ? ? for iii = 1:rnum
> > > ? ? ? ? ?X1=r(iii,1:3:end)';
> > > ? ? ? ? ?Y1=r(iii,2:3:end)';
> > > ? ? ? ? ?Z1=r(iii,3:3:end)';
> > > ? ? ? ? ?B1=[X1,Y1,Z1];
> > > ? ? end
>
> > > Thank you.
>
> > start out with B1 = [] and do B1 = [B1;X1,Y1,Z1];
>
> > B1 = [];
> > r = cell2mat(r);
> > rnum = numel(r(:,1));
> > for iii = 1:rnum
> >      X1=r(iii,1:3:end)';
> >      Y1=r(iii,2:3:end)';
> >      Z1=r(iii,3:3:end)';
> >      B1=[B1;X1,Y1,Z1];
> > end
>
> Hi,
>
> Thanks that worked. Although its telling me to preallocate B1?
>
> Regards,
>
> Jeremy

If you previously know the size of what B1 is supposed to be (in your
case 6174x3), do exactly what it says. Make B1 that big to begin with.
You have to change your code around a bit to make it still work,
however, but it will be faster with preallocation.

B1 = zeros(6174,3);
rnum = numel(r(:,1));
for iii = 1:rnum
     X1=r(iii,1:3:end)';
     Y1=r(iii,2:3:end)';
     Z1=r(iii,3:3:end)';
     B1((iii-1)*98+1:(iii)*98,1:3)=[X1,Y1,Z1]; %puts X1,Y1,Z1 into the
correct spots in B1
end