alan
alan

Reputation: 15

Vectorization in MATLAB

I'm trying to create a vector of size 121x101 such that the ith column is made up of V_t*e, where V_t = 1000*10^((i-1)/20) and e is a 121 long column of ones.

Clearly i is to be varied from 1 to 101 million, but how would I apply that to a matrix without only yielding the final value in the results (applying this to every column without repeating commands)?

Upvotes: 1

Views: 201

Answers (1)

Jonas
Jonas

Reputation: 74940

From your question, it looks like each row is the same. Thus, you can just calculate one row using REPMAT as

iRow = 1:101;
V_t = 1000*10.^((iRow-1)/20);
V_te = repmat(V_t,121,1);

If you want to have e be 1 in row 1, 2 in row 2, etc, you can use NDGRID to create two arrays of the same size as the output, which contain the values of e and i for every element (i,j) of the output

[ee,ii] = ndgrid(1:121,1:101);
V_te = 1000*10.^((i-1)/20) .* ee;

or you can use BSXFUN to do the expansion of e and i for you

iRow = 1:101;
V_t = 1000*10.^((iRow-1)/20);
V_te = bsxfun(@times,V_t,(1:121)');

Upvotes: 2

Related Questions