# Simple Matrix Manipulation Extressions Classic List Threaded 4 messages Open this post in threaded view
|

## Simple Matrix Manipulation Extressions

 Hello,       Are there any simple expressions for these octave expressions: In each case I introduce loops, multiplications or something a bit horrible to achieve something fairly simple. 1. given matrix A and row vector V , add V to every row of A         A + ones(rows(A),1) * V 2. similar problem for multiply:         A * diag(V)     or  A .* ones(rows(A),1) * V 3. given a matrix A and a scalar s, take the minimum of an element of the    matrix and the scalar.         (A < s) .* A + (s <= A) .* s) 4. given two same sized matrices A, B, find the pair-wise minimum of each    element:         (A < B) .* A + (A >= B) .* B     5. given a (big) matrix A, and (small) vector V, for each element a    of A find the number of V elements smaller than a         count = zeros(size(A)) ;         for r=1:n           count = count + (V(r) < A) ;         endfor    Is there a way to avoid the loop? we can sort the V if that helps.    For A a scalar we might do something like         count = sum( V < A ) ; 6. V is a vector, r is a matrix of integer indices into V. I seek to    build a matrix with the size of r, of elements of V       for i1=1:rows(r)          for i2=1:columns(r)            W(i1,i2) = V(r(i1,i2));          endfor       endfor     or (faster)       W = V(r) ;       W = reshape(W, rows(r), columns(r)) ; 7. rx and ry are same sized integer matrices, A is a matrix. I seek to    use r1 and r2 as indices into A to form a new matrix:       for i1 = 1:rows(rx)         for i2 = 1:columns(rx)           B(i1,i2) = A( rx(i1,i2)) , ry(i1,i2)) ) ;         endfor       endfor    The equivalent vector expression  W=V(r) is very simple.    The loops can be avoided by reshaping the matrices to vectors, working    in vectors, and reshaping back afterwards.   For each case I do have a solution, but generally I don't like it.         John Sorry if I should have RTFMed a bit more carefully.
Open this post in threaded view
|

## Simple Matrix Manipulation Extressions

 On  8-Nov-1997, john <[hidden email]> wrote: |       Are there any simple expressions for these octave expressions: | In each case I introduce loops, multiplications or something a bit | horrible to achieve something fairly simple. | | 1. given matrix A and row vector V , add V to every row of A | |         A + ones(rows(A),1) * V | | 2. similar problem for multiply: | |         A * diag(V) |     or  A .* ones(rows(A),1) * V Some future version of Octave will probably allow things like   A .+ V and   A .* V to do what you expect.  For now, you have to use a multiplication or indexing tricks to convert the vector to a matrix of the proper size. | 3. given a matrix A and a scalar s, take the minimum of an element of the |    matrix and the scalar. | |         (A < s) .* A + (s <= A) .* s) min (A, s) | 4. given two same sized matrices A, B, find the pair-wise minimum of each |    element: |         (A < B) .* A + (A >= B) .* B min (A, B) I don't know of better ways to do the other tasks you mention. jwe