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
blogical index describes column ofaneed select i.e.c = a(:,b). - unfortunately, logical vector
bvaries depending on processes. means number of columns incnot fixed. - some other processing use
cinputs , produce arraydhas same sizeci.e.size(d) == size(c) - now, need "reshape"
dhas same sizea. tricky part columns inaweren't chosen in #2 above needs replacednans. 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