help-octave
[Top][All Lists]

## FW: Matrix index, Please e-mail back.

 From: Jorgen Andersson Subject: FW: Matrix index, Please e-mail back. Date: Tue, 5 Dec 2006 06:17:22 -0800

Hi,

jwe asked me udse the help list....
I hope this is the help list...

Thanks
jma

Sent: Tue 12/5/2006 5:56 AM
Subject: RE: Matrix index, Please e-mail back.

jwe

| Hi John,

| How would I do what was intended:

| 2 4
| 3 8
| 9 3

| I want to index with [2,3,4] and then [3,4,5] ... [4,5,6] by letting
| i index the index range matrix. Can you do it with octave.
| My octave script works if in a loop. The loop is some slow
| so that is why I would like to use an index to the index ranges.

Please see below for: i , a , b.

| Best...
|  jma
|
| ________________________________
|
| From: John W. Eaton [mailto:address@hidden]
| Sent: Mon 12/4/2006 4:48 PM
| Subject: FW: Matrix index, Please e-mail back.
|
|
|
| On  4-Dec-2006, Jorgen Andersson wrote:
|
| | The Version is 2.1.73 Running on win XP with no command-line sw.
| |
| | ________________________________
| |
| | Sent: Mon 12/4/2006 11:24 AM
| | Subject: Matrix index, Please e-mail back.
| |
| |
| | Hi,
| |
| | I'm not sure, but depending on how indexing should work this could be a bug...
| |
| | Note how the second row in b = [3,5] , used as index numbers into "a"
| | the second column ought to be [4,8,3]  not [3,4,8] as last on this page.
| |
| | It looks like octave makes note of the first index and uses it and not [3,5]
| |
| | If this is not a bug and I do not want to use loops, how can I make the
| | index work as I planned it?
| |
| | Best...
| |
| | octave:300> i
| | i =
| |   1  2
| | octave:301> b
| | b =
| |   2  4
| |   3  5
| | octave:302> a
| | a =
| |   1  2  3  4  5
| |   2  3  4  5  6
| |   3  4  5  6  7
| |   9  8  7  6  5
| |   0  3  4  5  6
| | octave:303> a(b(i,1):b(i,2),i)
| | ans =
| |   2  3
| |   3  4
| |   9  8
|
| This is not a bug.
|
|   octave:1> i = [1, 2]
|   i =
|
|     1  2
|
|   octave:2> b = [2, 4; 3, 5]
|   b =
|
|     2  4
|     3  5
|
|   octave:3> b(i,1)
|   ans =
|
|     2
|     3
|
|   octave:4> b(i,2)
|   ans =
|
|     4
|     5
|
|   octave:5> b(i,1):b(i,2)
|   ans =
|
|     2  3  4
|
| This happens because when you make a range using matrix values, only
| the first element of the matrix values are used.  In other words, if
| M1 and M2 are matrices, then the range _expression_ M1:M2 is equivalent
| to M1(1):M2(2).  Octave has this "feature" for compatibility with
| Matlab.
|
| Finally, you are indexing the array A with
|
|  a([2,3,4], [1,2])
|
| and it looks like Octave gave you the right result for that operation.
|
| jwe
|
|
| <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
| <HTML>
|
| <META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7638.1">
| <TITLE>FW: Matrix index, Please e-mail back.</TITLE>
| <BODY>
| <DIV dir=ltr><FONT face=Arial color=#000000 size=2>Hi John,</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
| <DIV dir=ltr><FONT face=Arial size=2>How would I do what was
| intended:</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
| <DIV dir=ltr><FONT face=Arial size=2>2 4</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2>3 8 </FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2>9 3</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
| <DIV dir=ltr><FONT face=Arial size=2>I want to index with [2,3,4] and then
| [3,4,5] ... [4,5,6] by letting</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2>i index the index range matrix. Can you do
| it with octave.</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2>My octave script works if in a loop. The
| loop is some slow</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2>so that is why I would like to use an index
| to the index ranges.</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
| <DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
| <DIV dir=ltr><FONT face=Arial size=2>Best...</FONT></DIV>
| <DIV dir=ltr><FONT face=Arial size=2>&nbsp;jma</FONT></DIV></DIV>
| <DIV dir=ltr><BR>
| <HR tabIndex=-1>
| <FONT face=Tahoma size=2><B>From:</B> John W. Eaton
| back.<BR></FONT><BR></DIV>
| <DIV>
| <P><FONT size=2>On&nbsp; 4-Dec-2006, Jorgen Andersson wrote:<BR><BR>| The
| Version is 2.1.73 Running on win XP with no command-line sw.<BR>|<BR>|
| ________________________________<BR>|<BR>| From: Jorgen Andersson<BR>| Sent: Mon
| e-mail back.<BR>|<BR>|<BR>| Hi,<BR>|&nbsp;<BR>| I'm not sure, but depending on
| how indexing should work this could be a bug...<BR>|&nbsp;<BR>| Note how the
| second row in b = [3,5] , used as index numbers into "a"<BR>| the second column
| ought to be [4,8,3]&nbsp; not [3,4,8] as last on this page.<BR>|&nbsp;<BR>| It
| looks like octave makes note of the first index and uses it and not
| [3,5]<BR>|&nbsp;<BR>| If this is not a bug and I do not want to use loops, how
| can I make the<BR>| index work as I planned it?<BR>|&nbsp;<BR>|
| Best...<BR>|&nbsp; Jorgen Andersson<BR>|&nbsp;<BR>| octave:300&gt; i<BR>| i
| =<BR>|&nbsp;&nbsp; 1&nbsp; 2<BR>| octave:301&gt; b<BR>| b =<BR>|&nbsp;&nbsp;
| 2&nbsp; 4<BR>|&nbsp;&nbsp; 3&nbsp; 5<BR>| octave:302&gt; a<BR>| a
| =<BR>|&nbsp;&nbsp; 1&nbsp; 2&nbsp; 3&nbsp; 4&nbsp; 5<BR>|&nbsp;&nbsp; 2&nbsp;
| 3&nbsp; 4&nbsp; 5&nbsp; 6<BR>|&nbsp;&nbsp; 3&nbsp; 4&nbsp; 5&nbsp; 6&nbsp;
| 7<BR>|&nbsp;&nbsp; 9&nbsp; 8&nbsp; 7&nbsp; 6&nbsp; 5<BR>|&nbsp;&nbsp; 0&nbsp;
| 3&nbsp; 4&nbsp; 5&nbsp; 6<BR>| octave:303&gt; a(b(i,1):b(i,2),i)<BR>| ans
| =<BR>|&nbsp;&nbsp; 2&nbsp; 3<BR>|&nbsp;&nbsp; 3&nbsp; 4<BR>|&nbsp;&nbsp; 9&nbsp;
| 8<BR><BR>This is not a bug.<BR><BR>&nbsp; octave:1&gt; i = [1, 2]<BR>&nbsp; i
| =<BR><BR>&nbsp;&nbsp;&nbsp; 1&nbsp; 2<BR><BR>&nbsp; octave:2&gt; b = [2, 4; 3,
| 5]<BR>&nbsp; b =<BR><BR>&nbsp;&nbsp;&nbsp; 2&nbsp; 4<BR>&nbsp;&nbsp;&nbsp;
| 3&nbsp; 5<BR><BR>&nbsp; octave:3&gt; b(i,1)<BR>&nbsp; ans
| =<BR><BR>&nbsp;&nbsp;&nbsp; 2<BR>&nbsp;&nbsp;&nbsp; 3<BR><BR>&nbsp; octave:4&gt;
| b(i,2)<BR>&nbsp; ans =<BR><BR>&nbsp;&nbsp;&nbsp; 4<BR>&nbsp;&nbsp;&nbsp;
| 5<BR><BR>&nbsp; octave:5&gt; b(i,1):b(i,2)<BR>&nbsp; ans
| =<BR><BR>&nbsp;&nbsp;&nbsp; 2&nbsp; 3&nbsp; 4<BR><BR>This happens because when
| you make a range using matrix values, only<BR>the first element of the matrix
| values are used.&nbsp; In other words, if<BR>M1 and M2 are matrices, then the
| range _expression_ M1:M2 is equivalent<BR>to M1(1):M2(2).&nbsp; Octave has this
| "feature" for compatibility with<BR>Matlab.<BR><BR>Finally, you are indexing the
| array A with<BR><BR>&nbsp;a([2,3,4], [1,2])<BR><BR>and it looks like Octave gave
| you the right result for that
| operation.<BR><BR>jwe<BR></FONT></P></DIV>
|
| </BODY>
| </HTML>