How to calculate the average of each column without taking zeros values

Assume that there is a matrix:[1,3,5,0,0,0; 2,0,3,4,0,0; 3,0,0,3,0,1;4,2,2,1,8,2; 5,3,3,0,5,0; 6,7,0,0,2,0]. Display as follows
1 3 5 0 0 0
2 0 3 4 0 0
3 0 0 3 0 1
4 2 2 1 8 2
5 3 3 0 5 0
6 7 0 0 2 0
I want to calculate the average of each column without taking zeros values. The result is displayed in the corresponding column. what should I do?
Thanks in advance!

Antworten (3)

Hi @Amy, to calculate the average of each column in a matrix while ignoring zero values, you can use MATLAB's logical indexing and array operations. Here’s how you can achieve this:
  1. Create the Matrix: Define the matrix in MATLAB.
  2. Calculate Averages: Iterate over each column, exclude zeros, and compute the average.
  3. Display the Result: Store the averages and display them.
Here is how you can achieve this:
A = [1, 3, 5, 0, 0, 0;
2, 0, 3, 4, 0, 0;
3, 0, 0, 3, 0, 1;
4, 2, 2, 1, 8, 2;
5, 3, 3, 0, 5, 0;
6, 7, 0, 0, 2, 0];
averages = zeros(1, size(A, 2));
for col = 1:size(A, 2)
columnData = A(:, col);
nonZeroData = columnData(columnData ~= 0);
if ~isempty(nonZeroData)
averages(col) = mean(nonZeroData);
else
averages(col) = NaN;
end
end
disp('Matrix A:');
Matrix A:
disp(A);
1 3 5 0 0 0 2 0 3 4 0 0 3 0 0 3 0 1 4 2 2 1 8 2 5 3 3 0 5 0 6 7 0 0 2 0
disp('Column averages (ignoring zeros):');
Column averages (ignoring zeros):
disp(averages);
3.5000 3.7500 3.2500 2.6667 5.0000 1.5000
Now, you can correctly compute and display the average of non-zero elements for each column
Sandeep Mishra
Sandeep Mishra am 29 Nov. 2024
Bearbeitet: Sandeep Mishra am 29 Nov. 2024
Hi Amy,
To calculate the avergae of each column without taking the zero values, you can use the MATLAB 'sum' function to calculate the total number of non zero elements along with sum of all elements in each column.
You can refer to the following code snippet:
nonZeroElement = sum(matrix~=0)
totalSum = sum(matrix)
avg = totalSum./nonZeroElement
For more information, refer to the following MathWorks Documentation to learn about ‘sum’ function: https://www.mathworks.com/help/matlab/ref/double.sum.html
I hope this help you!
Try thiis —
matrix = [1,3,5,0,0,0; 2,0,3,4,0,0; 3,0,0,3,0,1;4,2,2,1,8,2; 5,3,3,0,5,0; 6,7,0,0,2,0]
matrix = 6×6
1 3 5 0 0 0 2 0 3 4 0 0 3 0 0 3 0 1 4 2 2 1 8 2 5 3 3 0 5 0 6 7 0 0 2 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
matrix(matrix == 0) = NaN
matrix = 6×6
1 3 5 NaN NaN NaN 2 NaN 3 4 NaN NaN 3 NaN NaN 3 NaN 1 4 2 2 1 8 2 5 3 3 NaN 5 NaN 6 7 NaN NaN 2 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean_matrix = mean(matrix, 'omitnan')
mean_matrix = 1×6
3.5000 3.7500 3.2500 2.6667 5.0000 1.5000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean_matrix = mean(matrix, 'omitmissing')
mean_matrix = 1×6
3.5000 3.7500 3.2500 2.6667 5.0000 1.5000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can use either 'omitmissing' or 'omitnan' as the ‘nanflag’
.

Tags

Gefragt:

Amy
am 29 Nov. 2024

Beantwortet:

am 29 Nov. 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by