matlab: vectorizing a single loop that is ORing N binary matrices -
the below code computation on data
matrix, data laid --
data = [ ... 1 2 3 4 5 6; ... 1 2 3 4 5 6; ... 1 2 3 4 5 6;]
and code running --
[~,col] = size(data) ; flag1 = bsxfun(@lt, data(:,1), data(:,1).'); flag2 = bsxfun(@gt, data(:,1), data(:,1).'); cindex = 2:col % can rid of loop ? flag1 = flag1 | bsxfun(@lt, data(:,cindex), data(:,cindex).'); flag2 = flag2 | bsxfun(@gt, data(:,cindex), data(:,cindex).'); end
what code doing comparing each row in column major order , creating 2 matrices of binary values flag1
, flag2
.
is there anyway rid of for cindex = 2:col
loop ?
you need permuting(rearrange dimensions)
create singleton dimensions
expansions take place when using bsxfun
later on, replace looping used in original posted code. so, implementation -
flag1 = any(bsxfun(@lt,permute(data,[1 3 2]),permute(data,[3 1 2])),3); flag2 = any(bsxfun(@gt,permute(data,[1 3 2]),permute(data,[3 1 2])),3);
Comments
Post a Comment