Path: news.mathworks.com!not-for-mail
From: "Emanuele " <emanuelemignosa.nospam@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: re-positioning a Matrix
Date: Thu, 22 May 2008 08:35:05 +0000 (UTC)
Organization: Bocconi
Lines: 66
Message-ID: <g13b7p$t99$1@fred.mathworks.com>
References: <g13865$nl6$1@fred.mathworks.com> <g139di$727$1@fred.mathworks.com>
Reply-To: "Emanuele " <emanuelemignosa.nospam@mathworks.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 1211445305 29993 172.30.248.37 (22 May 2008 08:35:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 22 May 2008 08:35:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1017421
Xref: news.mathworks.com comp.soft-sys.matlab:469812



"Jos " <DELjos@jasenDEL.nl> wrote in message <g139di$727
$1@fred.mathworks.com>...
> "Emanuele " <emanuelemignosa.nospam@mathworks.com> wrote 
in
> message <g13865$nl6$1@fred.mathworks.com>...
> > Hi all, during writing the code of an algorithms i have 
> > falled into a problem.
> > 
> > The result of math calculation is a matrix, of this 
type:
> > 
> > Matrix1 = [0   0   0   1    5  ]
> >           [0   0   0   0.1  0.2]
> >           [0   0   0   0.5  0.4]
> >           [0   0   0   0.3  0.1]
> > 
> > Where the first row is similar an index.  
> > I would re-positioning the matrix1 following the index 
of 
> > first row, in this way:
> > 
> > Matrix2 = [1   0   0   0    5  ]
> >           [0.1 0   0   0    0.2]
> >           [0.5 0   0   0    0.4]
> >           [0.3 0   0   0    0.1]
> > 
> > (Where the number of first row is the number of column)
> > There is a way to do it? I have no idea in this 
moment : - )
> > 
> > Thanks all in advace
> > 
> > Emanuele
> > 
> > 
> 
> 
> it is not that trivial:
> 
> % Example data
>   M = [ 0 0 0 1 5 ; 0 0 0 100 200 ; 0 0 0 10 20]
> 
> M2 = zeros(size(M)) ;
> c = M(1,:) 
> M2(:,c(c>0)) = M(:,c>0)
> 
> hth
> Jos
> 
Hi Jos, wow, your solution works and it's very elegant and 
fine, 
I belived that solution probably would be a for cicle, 
instead your solution is very fast.
It's very interesting, i want study it. : - )

I don't understand the last line of your code.
I don't know that Matlab accept code in his left part 
"M2(:,c(c>0))" without for cicle.

However tahnks very much.

Emanuele