matlab - Doing operations between subsequent elements without using a FOR loop? -


in matlab, possible simple operations between subsequent elements of array without using for loop? diff(). example, have vector:

a = [2 4 8 16 32] 

and want each element divided predecessor:

ans = [2 2 2 2] 

how can without going through elements (without using loops)?

you can use fact division in matlab work on both scalars , matrices, if use ./ operator rather /

>> = [2 4 8 16 32]; >> a(2:end) ./ a(1:end-1) ans =      2     2     2     2 

regarding question doing dot() between vectors stored in rows of matrix. there additional argument dot() tells whether vectors stored in columns (the default) or rows;

>> x = rand(3); >> y = rand(3);  # random vectors >> dot(x,y)      # dot product of column vectors ans =     0.5504    0.5561    0.5615 >> dot(x,y,2)    # dot product of row vectors ans =     0.3170     1.0938     0.2572 

most functions in matlab vectorized can work on scalars, vectors , matrices, have read documentation (e.g. type help dot) work out how use them.


Comments