Vectorization

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Vectorization

Thomas D. Dean-2
I have data collected from an optical encoder attached to a DC motor.
The motor is driver with a PWM signal.

PWM = [0:20:240 255 240: -20:-240 -255 -240:20:0]

 > size(encoder)
20571 1

I want mean(encoder(PWM==20)) for each of the PWM values.

How do I vectorize this?

Tom Dean

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

Re: Vectorization

Thomas D. Dean-2
On 10/07/14 21:12, Thomas D. Dean wrote:


Sorry, I have been looking at this too long.  I can iterate:

pvals=[0:20:240 255 240: -20:-240 -255 -240:20:0];
L=zeros(size(pvals));
R=L;
idx=0;
for p = pvals
   L(++idx) = mean(encoder(pwm == p));
end

 > size(Lpwm)
20571 1

 > size(encoder)
20571 1

Tom Dean

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

Re: Vectorization

Bård Skaflestad
In reply to this post by Thomas D. Dean-2
On Tue, 2014-10-07 at 21:12 -0700, Thomas D. Dean wrote:
> I have data collected from an optical encoder attached to a DC motor.
> The motor is driver with a PWM signal.
>
> PWM = [0:20:240 255 240: -20:-240 -255 -240:20:0]
>
>  > size(encoder)
> 20571 1
>
> I want mean(encoder(PWM==20)) for each of the PWM values.

Here's one *possible* option.  I'm not going to claim it's particularly
good because it generates an intermediate DOUBLE matrix of size

    NUMEL(UNIQUE(pvals))-by-NUMEL(encoder)

so you may want to look into a solution based on ACCUMARRAY or SPARSE
instead--especially if memory is tight.

        [up, iu, iu] = unique(pvals(:));
        v = double(up == reshape(pvals, 1, [])) ...
            * [ encoder(:), ones([numel(encoder), 1]) ];
        L = v(:,1) ./ v(:,2);
        L = reshape(L(iu), size(pvals));


Sincerely,
--
Bård Skaflestad <[hidden email]>
SINTEF ICT, Applied Mathematics


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

Re: Vectorization

Bård Skaflestad
On Wed, 2014-10-08 at 10:40 +0200, Bård Skaflestad wrote:

>         v = double(up == reshape(pvals, 1, [])) ...

Sorry.  That should, of course, read

        v = double(up == reshape(pvm, 1, [])) ...

Otherwise you get size mismatch.


Sincerely,
--
Bård Skaflestad <[hidden email]>
SINTEF ICT, Applied Mathematics


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