Thread Subject: re-positioning a Matrix

Subject: re-positioning a Matrix

From: Emanuele

Date: 22 May, 2008 07:43:02

Message: 1 of 4

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


Subject: re-positioning a Matrix

From: Jos

Date: 22 May, 2008 08:04:02

Message: 2 of 4

"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

Subject: re-positioning a Matrix

From: Emanuele

Date: 22 May, 2008 08:35:05

Message: 3 of 4

"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




Subject: re-positioning a Matrix

From: Jos

Date: 22 May, 2008 11:14:02

Message: 4 of 4

"Emanuele " <emanuelemignosa.nospam@mathworks.com> wrote in
message <g13b7p$t99$1@fred.mathworks.com>...
< SNIP .. trouble understanding (logical) indexing

Hi Emanuele,

If you break down the code "M2(:,c(c>0)) = M(:,c>0)" into
single lines, you might be able to follow

1) q = c > 0
returns a logical array where c is larger than zero

2) COLIND = c(q)
returns those elements in c that are larger than zero

3) M2(:,colind) selects only those columns specified by
COLIND (called "normal" indexing)

4) M(:,q) selects those columns for which q is true (called
logical indexing)

You can use indexing on both the lefthand or righthand side
of an assignment operator. For instance

X(3) = 2 sets the third element of X to 2

j = X(5) retrieves the fifth element of X

If you use them on the lefthand side you need to be sure
that the number of elements match:

X = [0 0 0 0] ;
X(2) = [2 3] ; error!

hth
Jos



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

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com