matlab - How to vectorize a for loop for a cell format matrix -
i have matrix in cell format, each element cell contains 2 numbers, example if c1 cell index 1, have c1 = (k1,k2). want operation each cell example this:
(k1-a) + (k2-b) the main operation is:
[(k1-a1)/(z1-a1) * (k1-a2)/(z1-a2) * ....] * [(k2-b1)/(z2-b1) * (k2-b2)/(z2-k2) * ...] but don't want use loop , think whole matrix once faster way.
how possible?
one approach -
num2cell(reshape(sum(bsxfun(@minus,vertcat(c{:}),[a b]),2),size(c))) %%// c input cell array edit 1: little pieces of information gather of question , comments, hoping need -
%%// **** inputs (sample values taken here demo). %%// replace these values ab = [{[1 0]} {[2 7]} {[2 2]} ; {[6 2]} {[7 6]} {[5 4]}]; %%// a-b cell array c1 = {[2 3]}; %%// c1 cell array z12 = {[4 3]}; %%// z12 cell array %%// ***** processing starts here %%// read in data double matrices c1mat = cell2mat(c1) k1 = c1mat(1); k2 = c1mat(2); z12mat = cell2mat(z12) z1 = z12mat(1); z2 = z12mat(2); abmat = cell2mat(ab); amat = abmat(:,1:2:end) bmat = abmat(:,2:2:end) amat = amat(:); bmat = bmat(:); %%// [(k1-a1)/(z1-a1) * (k1-a2)/(z1-a2) * ....] v1 = prod((k1-amat)./(z1-amat)) %%// [(k2-b1)/(z2-b1) * (k2-b2)/(z2-k2) * ...] v2 = prod((k2-bmat)./(z2-bmat)) %%// final output out = v1.*v2
Comments
Post a Comment