matlab - changing certain elements in a 2D array -
i'm working on algorithm in matlab requires elements of matrix updated regularly , looking how best this. here description of i'm trying achieve:
- i have mxn array
a
, 1xn vectorb
. - basically, vector
b
logical index describes column ofa
need select i.e.c = a(:,b)
. - unfortunately, logical vector
b
varies depending on processes. means number of columns inc
not fixed. - some other processing use
c
inputs , produce arrayd
has same sizec
i.e.size(d) == size(c)
- now, need "reshape"
d
has same sizea
. tricky part columns ina
weren't chosen in #2 above needs replacednan
s. of course can crude way of using loops. i'm looking matlab-way i.e. linear or logical indexing, vectorization, etc got stuck @ moment.
some examples make things clearer:
lets
a = [1 2 3; 4 5 6; 7 8 9] b = [1 0 1] c = a(:,b) = [1 3; 4 6; 7 9]
after processing, i'll d = [2 5; 6 7; 3 3]
. now, need "reshape" d
same size a
padding nan
i.e. d = [2 nan 5; 6 nan 7; 3 nan 3]
.
what i've tried far,
atmp = nan(size(a)); btmp = find(repmat(b,[size(b,1),1])); atmp(btmp) = d(btmp); %-> error because d smaller a.
how about
fulld = nan(size(a)); fulld(:, b) = d;
Comments
Post a Comment