Lots of ways to do this... perhaps the easiest would be running the 3dcalc command on each of your masks to merge it with the reference, and then using 3dBrickStat with -positive option to figure out which one to keep.
for aMask in mask*+tlrc.HEAD
do
3dcalc -a ReferenceMask+tlrc.HEAD -b ${aMask} -prefix merged_${aMask} -expr 'ispositive(a)*ispositive(b)'
done
max=0
max_dset=" "
for aMergedMask in merged_*+tlrc.HEAD
do
aValue=`3dBrickStat -slow -positive ${aMergedMask}`
if [ $aValue -gt $max ]; then
max=$aValue
max_dset=${aMergedMask}
fi
done
3dcopy $max_dset MaxWithReference
#optional cleanup
#rm merged*+tlrc.*
Some possibility of a typo, since I didn't run this exact code. But the debugging should be short.
Edited 1 time(s). Last edit at 09/03/2018 09:08AM by Peter Molfese.