[prev in list] [next in list] [prev in thread] [next in thread] 

List:       scilab-users
Subject:    Re: [scilab-Users] Using find on matrices
From:       "Eric Dubois" <grocer.toolbox () gmail ! com>
Date:       2008-04-08 15:32:44
Message-ID: b04eeae20804080832y762a0693h24ab66eb1a30a049 () mail ! gmail ! com
[Download RAW message or body]

What does the winner of this logical contest win? ;-)

Anyway, I have an exact and an approximate solution (if the only problem is
to avoid loops!):

- first problem:
--> A((jj-1)*size(A,1)+ii) = 5

- second problem:
--> B= matrix(A,-1,1)
--> C=matrix(B,-1,1) .*. ones(1,size(v,2)) - ones(size(C,1),1) .*. v
--> D=C(:,2:$) <0 & C(:,1:$-1) >=0
--> [f1,f2]=find(D)

which provides the result you want (although presented slightly diffrently,
but I do not know if it makes a difference for your uses).

Eric.


2008/4/7, harishankar ramachandran <hsr@ee.iitm.ac.in>:
>
> Hi all,
>
> If I have a matrix "A", and I apply a condition on it, I can extract the
> indices for which the condition is true, and I can then use that vector to
> change those values. For eg:
>
> indx=find(A>4);
> A(indx)=5;
>
> There is also a double index version of find, where I can get the (i,j)
> coordinates of the points. However, I do not seem to be able to use the
> following code:
>
> [ii,jj]=find(A>4);
> A(ii,jj)=5;
>
> This instead assigns 5 to a submatrix of A defined by rows ii and columns
> jj.
>
> What is the proper way to vectorially assign values to elements of a
> matrix A,
> if I have the element locations in the form [ii,jj]? Without a for loop,
> that
> is.
>
> Another question: Suppose I have a set of disjoint conditions with which I
> wish to partition a matrix. How do I extract the corresponding elements
> with
> a single command? This seems like a very useful capability, but I don't
> find
> anyway to do this without using a for loop. Eg:
>
> A=int(rand(5,5)*12);
> v=0:2:12;
> l=list()
> for k=1:length(v)-1
> l(k)=find(A>=v(k) & A<v(k+1));
> end
>
> This yields:
> A  =
>
>    2.    7.     6.    2.     3.
>    9.    10.    7.    2.     11.
>    0.    8.     8.    2.     2.
>    3.    10.    2.    10.    3.
>    7.    0.     6.    7.     4.
>
> l  =
>    l(1) = 3.    10.
>    l(2) = 1.    4.    14.    16.    17.    18.    21.    23.    24.
>    l(3) = 25.
>    l(4) = 5.    6.    11.    12.    15.    20.
>    l(5) = 2.    8.    13.
>    l(6) = 7.    9.    19.    22.
>
> But can I do it faster, without for loops? I would like to have a command
> that
> does:
>
> A=int(rand(5,5)*12);
> v=0:2:12;
> l=findmany(A>=v(1:$-1) & A<v(2:$));
>
> or something like that. Is there some such thing?
>
> Thanks in advance
>
> hari ramachandran
> --
> Dr. Hari Ramachandran, EE Dept, IIT-Madras
>

[Attachment #3 (text/html)]

<div>What does the winner of this logical contest win? ;-)</div>
<div>&nbsp;</div>
<div>Anyway, I have an exact and an approximate&nbsp;solution (if the only problem is \
to avoid loops!):</div> <div>&nbsp;</div>
<div>- first problem: </div>
<div>--&gt; A((jj-1)*size(A,1)+ii) = 5</div>
<div>&nbsp;</div>
<div>- second problem:</div>
<div>--&gt; B= matrix(A,-1,1)</div>
<div>--&gt; C=matrix(B,-1,1) .*. ones(1,size(v,2)) - ones(size(C,1),1) .*. v</div>
<div>--&gt; D=C(:,2:$) &lt;0 &amp; C(:,1:$-1) &gt;=0</div>
<div>--&gt; [f1,f2]=find(D)</div>
<div>&nbsp;</div>
<div>which provides the result you want (although presented slightly diffrently, but \
I do not know if it makes a difference for your uses).</div> <div>&nbsp;</div>
<div>Eric.<br><br>&nbsp;</div>
<div><span class="gmail_quote">2008/4/7, harishankar ramachandran &lt;<a \
href="mailto:hsr@ee.iitm.ac.in">hsr@ee.iitm.ac.in</a>&gt;:</span> <blockquote \
class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: \
#ccc 1px solid">Hi all,<br><br>If I have a matrix &quot;A&quot;, and I apply a \
condition on it, I can extract the<br>indices for which the condition is true, and I \
can then use that vector to<br> change those values. For \
eg:<br><br>indx=find(A&gt;4);<br>A(indx)=5;<br><br>There is also a double index \
version of find, where I can get the (i,j)<br>coordinates of the points. However, I \
do not seem to be able to use the<br> following \
code:<br><br>[ii,jj]=find(A&gt;4);<br>A(ii,jj)=5;<br><br>This instead assigns 5 to a \
submatrix of A defined by rows ii and columns jj.<br><br>What is the proper way to \
vectorially assign values to elements of a matrix A,<br> if I have the element \
locations in the form [ii,jj]? Without a for loop, that<br>is.<br><br>Another \
question: Suppose I have a set of disjoint conditions with which I<br>wish to \
partition a matrix. How do I extract the corresponding elements with<br> a single \
command? This seems like a very useful capability, but I don&#39;t find<br>anyway to \
do this without using a for loop. \
Eg:<br><br>A=int(rand(5,5)*12);<br>v=0:2:12;<br>l=list()<br>for \
k=1:length(v)-1<br>l(k)=find(A&gt;=v(k) &amp; A&lt;v(k+1));<br> end<br><br>This \
yields:<br>A&nbsp;&nbsp;=<br><br>&nbsp;&nbsp; \
2.&nbsp;&nbsp;&nbsp;&nbsp;7.&nbsp;&nbsp;&nbsp;&nbsp; \
6.&nbsp;&nbsp;&nbsp;&nbsp;2.&nbsp;&nbsp;&nbsp;&nbsp; 3.<br>&nbsp;&nbsp; \
9.&nbsp;&nbsp;&nbsp;&nbsp;10.&nbsp;&nbsp;&nbsp;&nbsp;7.&nbsp;&nbsp;&nbsp;&nbsp;2.&nbsp;&nbsp;&nbsp;&nbsp; \
11.<br>&nbsp;&nbsp; 0.&nbsp;&nbsp;&nbsp;&nbsp;8.&nbsp;&nbsp;&nbsp;&nbsp; \
8.&nbsp;&nbsp;&nbsp;&nbsp;2.&nbsp;&nbsp;&nbsp;&nbsp; 2.<br>&nbsp;&nbsp; \
3.&nbsp;&nbsp;&nbsp;&nbsp;10.&nbsp;&nbsp;&nbsp;&nbsp;2.&nbsp;&nbsp;&nbsp;&nbsp;10.&nbsp;&nbsp;&nbsp;&nbsp;3.<br>&nbsp;&nbsp; \
7.&nbsp;&nbsp;&nbsp;&nbsp;0.&nbsp;&nbsp;&nbsp;&nbsp; \
6.&nbsp;&nbsp;&nbsp;&nbsp;7.&nbsp;&nbsp;&nbsp;&nbsp; 4.<br><br>l&nbsp;&nbsp;=<br> \
&nbsp;&nbsp; l(1) = 3.&nbsp;&nbsp;&nbsp;&nbsp;10.<br>&nbsp;&nbsp; l(2) = \
1.&nbsp;&nbsp;&nbsp;&nbsp;4.&nbsp;&nbsp;&nbsp;&nbsp;14.&nbsp;&nbsp;&nbsp;&nbsp;16.&nbs \
p;&nbsp;&nbsp;&nbsp;17.&nbsp;&nbsp;&nbsp;&nbsp;18.&nbsp;&nbsp;&nbsp;&nbsp;21.&nbsp;&nbsp;&nbsp;&nbsp;23.&nbsp;&nbsp;&nbsp;&nbsp;24.<br>&nbsp;&nbsp; \
l(3) = 25.<br>&nbsp;&nbsp; l(4) = \
5.&nbsp;&nbsp;&nbsp;&nbsp;6.&nbsp;&nbsp;&nbsp;&nbsp;11.&nbsp;&nbsp;&nbsp;&nbsp;12.&nbsp;&nbsp;&nbsp;&nbsp;15.&nbsp;&nbsp;&nbsp;&nbsp;20.<br>&nbsp;&nbsp; \
l(5) = 2.&nbsp;&nbsp;&nbsp;&nbsp;8.&nbsp;&nbsp;&nbsp;&nbsp;13.<br>&nbsp;&nbsp; l(6) = \
7.&nbsp;&nbsp;&nbsp;&nbsp;9.&nbsp;&nbsp;&nbsp;&nbsp;19.&nbsp;&nbsp;&nbsp;&nbsp;22.<br>
 <br>But can I do it faster, without for loops? I would like to have a command \
that<br>does:<br><br>A=int(rand(5,5)*12);<br>v=0:2:12;<br>l=findmany(A&gt;=v(1:$-1) \
&amp; A&lt;v(2:$));<br><br>or something like that. Is there some such thing?<br> \
<br>Thanks in advance<br><br>hari ramachandran<br>--<br>Dr. Hari Ramachandran, EE \
Dept, IIT-Madras<br></blockquote></div><br>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic