1

I am confused on how to vectorize the given code in Octave. Someone please lend me help. Thank you

function Z = expand(xx,ncol)
    xx = xx(:);
    Z = zeros(length(xx), ncol);
    for i = 1:ncol
        Z(:,i) = xx;
    endfor
    f = 1:5
 endfunction

Calling the function in the command window:

expand(f,2)

Result:

f =

  1   2   3   4   5

ans =

  2   2
  3   3
  4   4
  5   5
  6   6
  7   7
  8   8
  9   9
  • 3
    That's a rather funny function... you do realize that the `f` in `expand(f,2)` is most likely not the same `f` that gets printed to the standard output, right? Anyway, your entire function except for the `f=1:5,` statement at the end can be expressed in terms of an efficient built-in function, `repmat()` ([Octave](https://octave.sourceforge.io/octave/function/repmat.html) - [MATLAB](https://www.mathworks.com/help/matlab/ref/repmat.html)): `repmat(xx(:),[1 ncol]);` – Vicky Mar 21 '21 at 07:25
  • 1
    I think I made a mistake in copying the results. By the way, thank you for you answer! – Asher Remigio Mar 21 '21 at 10:17

0 Answers0