Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorise a reshape loop
Date: Thu, 17 Jul 2008 11:13:02 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 117
Message-ID: <g5n9fu$6vv$1@fred.mathworks.com>
References: <g5krk2$77v$1@fred.mathworks.com> <g5kthu$svb$1@fred.mathworks.com> <g5n4kd$jtp$1@fred.mathworks.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1216293182 7167 172.30.248.37 (17 Jul 2008 11:13:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 17 Jul 2008 11:13:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:479945



"Dave Brackett" <davebrackett@hotmail.com> wrote in message 
<g5n4kd$jtp$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in
> message <g5kthu$svb$1@fred.mathworks.com>...
> > "Dave Brackett" <davebrackett@hotmail.com> wrote in message 
> > <g5krk2$77v$1@fred.mathworks.com>...
> > > Hi, I am having trouble vectorising the below for loop. Can
> > > anyone help me please? I am expecting M to consist of 3 rows
> > > of 2x2 matrices each with 16 deep. Thanks in advance.
> > 
> > Try that again? What is the shape of M?
> > Is it 3x2x2x16? This is inconsistent with
> > what you have shown below.
> > 
> > 
> > > for p=1:3
> > > q=p;
> > > M=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);
> > > end
> > > 
> > > where a,b,c,d are 3x16 matrice such as: 
> > > (imaginary parts can be ignored)
> > >    1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i
> > 
> > Um, that is a 16x3 matrix.
> > 
> > Anyway, what loop?
> > 
> > Since you overwrite M on each pass
> > through, that non-loop does nothing
> > but define M at the last value of p.
> > 
> > We can't help you unless you will think
> > clearly enough about what you want to
> > explain it.
> > 
> > You might start by preallocating M to be
> > the proper size. Use zeros.
> > 
> > John
> > 
> > 
> 
> Ok, sorry for any confusion, I will try again.
> 
> I want a 3D matrix, consisting of in this case, 3 2x2
> matrices (as shown below) plus 15 additional 2x2 matrices
> stacked behind them, giving 3 rows of 16 2x2 matrices all
> contained within one matrix.

Do you want a 4-d matrix, that is 2x2x3x16?

Or does this totally unclear explanation
request a 2x2x48 matrix? What do you
mean when you say 3 rows 2x2 matrices?
Does this ask for a 3x2x2 array? Or for a
2x2x3 array?

For some reason, you do not accept what
it is you are trying to do, nor have you
taken the time or effort to explain it clearly.
Think clearly. If you want a single matrix
that contains 3 sets of 2x2 matrices, 16
times, then you have a 3x2x2x16 matrix.
A 4-d matrix.

Is this what you want?


> [a1 b1  [a2 b2  [a3 b3
>  c1 d1]  c2 d2]  c3 d3]
> 
> The amended 'for' loop shown below intends to do this. I
> have changed it so M should store each 2x2 matrix, but it is
> isn't right as I'm not sure how to do it right.
> 
> for p=1:3
> q=p;
> r=1:16;
> M(:,q,r)=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);
> end

Again, this loop merely (tries to) overwrite
M on each pass through. Is this what you
want? I don't think so from your description.

 
> As you rightly point out a,b,c, and d are indeed 16x3
> matrices. Preallocation of M can come later.

No. Preallocation should come FIRST, for
several good reasons. Not the least of
which is your acceptance of what you are
trying to do.

 
> Hopefully that explanation is a bit clearer. Thanks.

Clear now. As mud.

What is your goal? If it is as I have suggested
it might be, then you can do everything in
one line. I'll do it in two lines to make it
more understandable.

M = reshape([a(:),b(:),c(:),d(:)],16,3,2,2);
M = permute(M,[3 4 2 1]);

This will create a single 2x2x3x16 array.

So far, I still have no idea if this is what
you really want. So slow down. Think
clearly about what you are doing. Then
explain it clearly. Communication is a
valuable skill. Learn it.

John