help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applying each element of a vector to each row of a matrix


From: Jonathan Webster
Subject: Re: Applying each element of a vector to each row of a matrix
Date: Mon, 20 Dec 2010 02:07:29 -0500
User-agent: Thunderbird 2.0.0.24 (X11/20101213)

Jordi Gutiérrez Hermoso wrote:
On 19 December 2010 14:59, Jonathan Webster <address@hidden> wrote:
Having done a long stint at work using APL, and wanting to save my time
rather than the computer's time, I long ago wrote rho.m , implementing
the arbitrary  reshape properties of the APL operator of that name.  I
can supply it if anyone is interested.

Yes please. More code for the sharin' (and I promise I'll get around
some day to finishing that website that should make this kind of
sharing easier).

- Jordi G. H.

Here are further examples:
octave:3> vex = 1:3
vex =

 1  2  3

octave:4> mm = rho ( 2,3,3)
mm =

  2   2   2
  2   2   2
  2   2   2

octave:5> mmm = mm .^ rho(vex,3,3)
mmm =

  2   4   8
  2   4   8
  2   4   8

octave:6> (rho ( 2,3,3) .^ rho((1:3),3,3))
ans =

  2   4   8
  2   4   8
  2   4   8

Be well,
Jonathan
function [arout,ver] =  rho( arin,ny,nx)
%  function arout =  rho( arin,ny,nx)
%  inslpired in name and function by the APL operator of the same name
%  does NOT use Fortran indexing: fills rows first

%author: JCWebster
%cdate: 4/27/2000
%purpose: utility routine like the APL operator 'rho' 
%modified: 6/15/2000 to add revision number
ver = ["RHO","1.2"];

[nyi,nxi]= size(arin);
if (nyi >= 1) 
        arin = reshape (arin,1,(nyi*nxi));
endif

nnTot = ny * nx ; % total number of elemenents in output
arout = zeros(1,nnTot);
lengIn = length( arin); % length of input
ii = 0; % counter in input arg
for iii = 1:nnTot
ii++;
arout(1,iii) = arin(1,ii);
if(ii>=lengIn)
ii=0;
end
end

arout = reshape(arout, nx,ny)';

reply via email to

[Prev in Thread] Current Thread [Next in Thread]