MATLAB: Putting Stereo in Mono Acoustic Signal

arraymatrixmonononzerostereozero

I have the following piece of Matlab code trying to do a rough estimate for putting stereo signals into a mono signal.
%Putting in Mono
z1 = (y1(:,1)+y1(:,2))/2;
z2 = (y2(:,1)+y2(:,2))/2;
z3 = (y3(:,1)+y3(:,2))/2;
However, some of my values contain zeros, and I do not want to average a row if it has a zero; I would want to add these.
Only if there are two nonzero values would I want to average. How would I write this?
I apologize, I am very new to Matlab.

Best Answer

One approach:
stereo = randi([0 8], 15, 2); % Signal
lv = ~sum(stereo == 0, 2); % Rows Without Zeros (Logical Vector)
mono = sum(stereo, 2); % Sum Across Columns
mono(lv) = mono(lv)/2; % Divide Rows Without Zeros By ‘2’ To Get Mean