matlab - Finding the minimum of corresponding elements in four matrices -
so need find minimum value of corresponding elements in multiple matrices. did find this page extend question little further. matrices has positive , negative values (and zeros) , want find minimum values excluding zeros.
example:
z(:,:,1) = [-5 0 5 0 0 0 1 0 3]; z(:,:,2) = [1 0 2 0 0 0 0 0 0]; z(:,:,3) = [0 0 0 -9 0 4 0 0 0]; z(:,:,4) = [0 0 0 -2 0 0 0 0 0];
here's i'm using of now:
zmin = min(z,[],3);
but gives me:
[-5 0 0 -9 0 0 0 0 0]
but want result be:
[-5 0 2 -9 0 4 1 0 3]
any ideas? when utilize nonzeros
, messes everything.
here's workaround:
replace 0s in z nan, calculate min, switch 0:
clear clc close z(:,:,1) = [-5 0 5 0 0 0 1 0 3]; z(:,:,2) = [1 0 2 0 0 0 0 0 0]; z(:,:,3) = [0 0 0 -9 0 4 0 0 0]; z(:,:,4) = [0 0 0 -2 0 0 0 0 0]; %// assign nan 0 elements z(z ==0) = nan; zmin = min(z,[],3); %// switch 0 zmin(isnan(zmin)) = 0; %// same z; z(isnan(z)) =0;
the output looks this:
zmin z zmin = -5 0 2 -9 0 4 1 0 3 z(:,:,1) = -5 0 5 0 0 0 1 0 3 z(:,:,2) = 1 0 2 0 0 0 0 0 0 z(:,:,3) = 0 0 0 -9 0 4 0 0 0 z(:,:,4) = 0 0 0 -2 0 0 0 0 0
matlab matrix minimum
No comments:
Post a Comment