<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169714</link>
    <title>MATLAB Central Newsreader - re-positioning a Matrix</title>
    <description>Feed for thread: re-positioning a Matrix</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 22 May 2008 07:43:02 -0400</pubDate>
      <title>re-positioning a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169714#433461</link>
      <author>Emanuele </author>
      <description>Hi all, during writing the code of an algorithms i have &lt;br&gt;
falled into a problem.&lt;br&gt;
&lt;br&gt;
The result of math calculation is a matrix, of this type:&lt;br&gt;
&lt;br&gt;
Matrix1 = [0   0   0   1    5  ]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[0   0   0   0.1  0.2]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[0   0   0   0.5  0.4]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[0   0   0   0.3  0.1]&lt;br&gt;
&lt;br&gt;
Where the first row is similar an index.  &lt;br&gt;
I would re-positioning the matrix1 following the index of &lt;br&gt;
first row, in this way:&lt;br&gt;
&lt;br&gt;
Matrix2 = [1   0   0   0    5  ]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[0.1 0   0   0    0.2]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[0.5 0   0   0    0.4]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[0.3 0   0   0    0.1]&lt;br&gt;
&lt;br&gt;
(Where the number of first row is the number of column)&lt;br&gt;
There is a way to do it? I have no idea in this moment : - )&lt;br&gt;
&lt;br&gt;
Thanks all in advace&lt;br&gt;
&lt;br&gt;
Emanuele</description>
    </item>
    <item>
      <pubDate>Thu, 22 May 2008 08:04:02 -0400</pubDate>
      <title>re-positioning a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169714#433463</link>
      <author>Jos </author>
      <description>&quot;Emanuele &quot; &amp;lt;emanuelemignosa.nospam@mathworks.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;g13865$nl6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi all, during writing the code of an algorithms i have &lt;br&gt;
&amp;gt; falled into a problem.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The result of math calculation is a matrix, of this type:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Matrix1 = [0   0   0   1    5  ]&lt;br&gt;
&amp;gt;           [0   0   0   0.1  0.2]&lt;br&gt;
&amp;gt;           [0   0   0   0.5  0.4]&lt;br&gt;
&amp;gt;           [0   0   0   0.3  0.1]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Where the first row is similar an index.  &lt;br&gt;
&amp;gt; I would re-positioning the matrix1 following the index of &lt;br&gt;
&amp;gt; first row, in this way:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Matrix2 = [1   0   0   0    5  ]&lt;br&gt;
&amp;gt;           [0.1 0   0   0    0.2]&lt;br&gt;
&amp;gt;           [0.5 0   0   0    0.4]&lt;br&gt;
&amp;gt;           [0.3 0   0   0    0.1]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (Where the number of first row is the number of column)&lt;br&gt;
&amp;gt; There is a way to do it? I have no idea in this moment : - )&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks all in advace&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Emanuele&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
it is not that trivial:&lt;br&gt;
&lt;br&gt;
% Example data&lt;br&gt;
&amp;nbsp;&amp;nbsp;M = [ 0 0 0 1 5 ; 0 0 0 100 200 ; 0 0 0 10 20]&lt;br&gt;
&lt;br&gt;
M2 = zeros(size(M)) ;&lt;br&gt;
c = M(1,:) &lt;br&gt;
M2(:,c(c&amp;gt;0)) = M(:,c&amp;gt;0)&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Thu, 22 May 2008 08:35:05 -0400</pubDate>
      <title>re-positioning a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169714#433466</link>
      <author>Emanuele </author>
      <description>&quot;Jos &quot; &amp;lt;DELjos@jasenDEL.nl&amp;gt; wrote in message &amp;lt;g139di$727&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Emanuele &quot; &amp;lt;emanuelemignosa.nospam@mathworks.com&amp;gt; wrote &lt;br&gt;
in&lt;br&gt;
&amp;gt; message &amp;lt;g13865$nl6$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi all, during writing the code of an algorithms i have &lt;br&gt;
&amp;gt; &amp;gt; falled into a problem.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The result of math calculation is a matrix, of this &lt;br&gt;
type:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Matrix1 = [0   0   0   1    5  ]&lt;br&gt;
&amp;gt; &amp;gt;           [0   0   0   0.1  0.2]&lt;br&gt;
&amp;gt; &amp;gt;           [0   0   0   0.5  0.4]&lt;br&gt;
&amp;gt; &amp;gt;           [0   0   0   0.3  0.1]&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Where the first row is similar an index.  &lt;br&gt;
&amp;gt; &amp;gt; I would re-positioning the matrix1 following the index &lt;br&gt;
of &lt;br&gt;
&amp;gt; &amp;gt; first row, in this way:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Matrix2 = [1   0   0   0    5  ]&lt;br&gt;
&amp;gt; &amp;gt;           [0.1 0   0   0    0.2]&lt;br&gt;
&amp;gt; &amp;gt;           [0.5 0   0   0    0.4]&lt;br&gt;
&amp;gt; &amp;gt;           [0.3 0   0   0    0.1]&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; (Where the number of first row is the number of column)&lt;br&gt;
&amp;gt; &amp;gt; There is a way to do it? I have no idea in this &lt;br&gt;
moment : - )&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks all in advace&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Emanuele&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; it is not that trivial:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Example data&lt;br&gt;
&amp;gt;   M = [ 0 0 0 1 5 ; 0 0 0 100 200 ; 0 0 0 10 20]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; M2 = zeros(size(M)) ;&lt;br&gt;
&amp;gt; c = M(1,:) &lt;br&gt;
&amp;gt; M2(:,c(c&amp;gt;0)) = M(:,c&amp;gt;0)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; hth&lt;br&gt;
&amp;gt; Jos&lt;br&gt;
&amp;gt; &lt;br&gt;
Hi Jos, wow, your solution works and it's very elegant and &lt;br&gt;
fine, &lt;br&gt;
I belived that solution probably would be a for cicle, &lt;br&gt;
instead your solution is very fast.&lt;br&gt;
It's very interesting, i want study it. : - )&lt;br&gt;
&lt;br&gt;
I don't understand the last line of your code.&lt;br&gt;
I don't know that Matlab accept code in his left part &lt;br&gt;
&quot;M2(:,c(c&amp;gt;0))&quot; without for cicle.&lt;br&gt;
&lt;br&gt;
However tahnks very much.&lt;br&gt;
&lt;br&gt;
Emanuele</description>
    </item>
    <item>
      <pubDate>Thu, 22 May 2008 11:14:02 -0400</pubDate>
      <title>re-positioning a Matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169714#433491</link>
      <author>Jos </author>
      <description>&quot;Emanuele &quot; &amp;lt;emanuelemignosa.nospam@mathworks.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;g13b7p$t99$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;lt; SNIP .. trouble understanding (logical) indexing&lt;br&gt;
&lt;br&gt;
Hi Emanuele,&lt;br&gt;
&lt;br&gt;
If you break down the code &quot;M2(:,c(c&amp;gt;0)) = M(:,c&amp;gt;0)&quot; into&lt;br&gt;
single lines, you might be able to follow&lt;br&gt;
&lt;br&gt;
1) q = c &amp;gt; 0&lt;br&gt;
returns a logical array where c is larger than zero&lt;br&gt;
&lt;br&gt;
2) COLIND = c(q)&lt;br&gt;
returns those elements in c that are larger than zero&lt;br&gt;
&lt;br&gt;
3) M2(:,colind) selects only those columns specified by&lt;br&gt;
COLIND (called &quot;normal&quot; indexing)&lt;br&gt;
&lt;br&gt;
4) M(:,q) selects those columns for which q is true (called&lt;br&gt;
logical indexing)&lt;br&gt;
&lt;br&gt;
You can use indexing on both the lefthand or righthand side&lt;br&gt;
of an assignment operator. For instance&lt;br&gt;
&lt;br&gt;
X(3) = 2 sets the third element of X to 2&lt;br&gt;
&lt;br&gt;
j = X(5) retrieves the fifth element of X&lt;br&gt;
&lt;br&gt;
If you use them on the lefthand side you need to be sure&lt;br&gt;
that the number of elements match:&lt;br&gt;
&lt;br&gt;
X = [0 0 0 0] ;&lt;br&gt;
X(2) = [2 3] ;  error!&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos </description>
    </item>
  </channel>
</rss>

