[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Simple Matrix Manipulation Extressions
From: |
john |
Subject: |
Simple Matrix Manipulation Extressions |
Date: |
Sat, 8 Nov 1997 10:55:09 +0000 (GMT) |
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.
- Simple Matrix Manipulation Extressions,
john <=