Please, help me vectorize this code

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Please, help me vectorize this code

Pablo Fernandez
Hi,

I want the elements of a matrix M_ to be equal to those of an original
matrix M but only if they satisfy a certain condition, otherwise write a
zero.

The following lines of code work as expected but I want to avoid the
loop if it is possible.

 >> M = floor(rand(5, 4)*10);
 >> [r, c] = find(M > 4);
 >> M_ = zeros(5, 4);
 >> for n = 1:length(r)
M_(r(n), c(n)) = M(r(n), c(n));
endfor

I have tried

M_ = M(find(M>4));

but that returns a column vector of the elements which satisfy the
condition.

Thanks in advance,

Pablo




_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Please, help me vectorize this code

Siki Zoltan
Dear Pablo,

instead of the loop write simply:

M_(r,c) = M(r,c);

Regards,
Zoltan

On Sun, 17 Sep 2017, Pablo Fernández Bustamante wrote:

> Hi,
>
> I want the elements of a matrix M_ to be equal to those of an original matrix
> M but only if they satisfy a certain condition, otherwise write a zero.
>
> The following lines of code work as expected but I want to avoid the
> loop if it is possible.
>
>>> M = floor(rand(5, 4)*10);
>>> [r, c] = find(M > 4);
>>> M_ = zeros(5, 4);
>>> for n = 1:length(r)
> M_(r(n), c(n)) = M(r(n), c(n));
> endfor
>
> I have tried
>
> M_ = M(find(M>4));
>
> but that returns a column vector of the elements which satisfy the condition.
>
> Thanks in advance,
>
> Pablo
>
>
>
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>
_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Please, help me vectorize this code

Michele Ginesi
In reply to this post by Pablo Fernandez
On 09/17/2017 01:15 PM, Pablo Fernández Bustamante wrote:

> Hi,
>
> I want the elements of a matrix M_ to be equal to those of an original
> matrix M but only if they satisfy a certain condition, otherwise write
> a zero.
>
> The following lines of code work as expected but I want to avoid the
> loop if it is possible.
>
> >> M = floor(rand(5, 4)*10);
> >> [r, c] = find(M > 4);
> >> M_ = zeros(5, 4);
> >> for n = 1:length(r)
> M_(r(n), c(n)) = M(r(n), c(n));
> endfor
>
> I have tried
>
> M_ = M(find(M>4));
>
> but that returns a column vector of the elements which satisfy the
> condition.
>
> Thanks in advance,
>
> Pablo
>
>
>
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave


You can do like this:

M = floor(rand(5,4)*10);
M1 = zeros(5,4);
M1(M>4) = M(M>4);

Bests,

--
Michele Ginesi


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Please, help me vectorize this code

gciriani
The correct code is
M .* (M>4)



On Sun, Sep 17, 2017 at 10:08 AM, michele <[hidden email]> wrote:
On 09/17/2017 01:15 PM, Pablo Fernández Bustamante wrote:
Hi,

I want the elements of a matrix M_ to be equal to those of an original matrix M but only if they satisfy a certain condition, otherwise write a zero.

The following lines of code work as expected but I want to avoid the
loop if it is possible.

>> M = floor(rand(5, 4)*10);
>> [r, c] = find(M > 4);
>> M_ = zeros(5, 4);
>> for n = 1:length(r)
M_(r(n), c(n)) = M(r(n), c(n));
endfor

I have tried

M_ = M(find(M>4));

but that returns a column vector of the elements which satisfy the condition.

Thanks in advance,

Pablo




_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave


You can do like this:

M = floor(rand(5,4)*10);
M1 = zeros(5,4);
M1(M>4) = M(M>4);

Bests,

--
Michele Ginesi



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Giovanni Ciriani - Windows 10, Octave 4.2.1, configured for x86_64-w64-mingw32
Reply | Threaded
Open this post in threaded view
|

Re: Please, help me vectorize this code

Pablo Fernandez


17/09/17 16:45(e)an, Giovanni Ciriani igorleak idatzi zuen:
> The correct code is
> M .* (M>4)
>

Thank you, Giovanni! This is so much simpler and faster!

Thank you, Michele and Zoltan.

Regards,
Pablo


>
> On Sun, Sep 17, 2017 at 10:08 AM, michele <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     On 09/17/2017 01:15 PM, Pablo Fernández Bustamante wrote:
>
>         Hi,
>
>         I want the elements of a matrix M_ to be equal to those of an
>         original matrix M but only if they satisfy a certain condition,
>         otherwise write a zero.
>
>         The following lines of code work as expected but I want to avoid the
>         loop if it is possible.
>
>          >> M = floor(rand(5, 4)*10);
>          >> [r, c] = find(M > 4);
>          >> M_ = zeros(5, 4);
>          >> for n = 1:length(r)
>         M_(r(n), c(n)) = M(r(n), c(n));
>         endfor
>
>         I have tried
>
>         M_ = M(find(M>4));
>
>         but that returns a column vector of the elements which satisfy
>         the condition.
>
>         Thanks in advance,
>
>         Pablo
>
>
>
>
>         _______________________________________________
>         Help-octave mailing list
>         [hidden email] <mailto:[hidden email]>
>         https://lists.gnu.org/mailman/listinfo/help-octave
>         <https://lists.gnu.org/mailman/listinfo/help-octave>
>
>
>
>     You can do like this:
>
>     M = floor(rand(5,4)*10);
>     M1 = zeros(5,4);
>     M1(M>4) = M(M>4);
>
>     Bests,
>
>     --
>     Michele Ginesi
>
>
>
>     _______________________________________________
>     Help-octave mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://lists.gnu.org/mailman/listinfo/help-octave
>     <https://lists.gnu.org/mailman/listinfo/help-octave>
>
>
>
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
>

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave