필터 지우기
필터 지우기

How to find the mean of a column in a matrix using for loop?

조회 수: 1 (최근 30일)
Hunter Nau
Hunter Nau 2022년 4월 19일
댓글: KSSV 2022년 4월 19일
For part B I have to use for loop to find the average of each column. I am getting the correct output but I am getting 5 of the same outputs. For part C I need to find the mean of all the temperatures, but I keep getting answers from 1:5.
%% Part A
TempSenVal=[58.3 59.2 60.2 63.0 63.3; 62.5 63.8 64.9 65.2 65.8;...
63.2 64.2 65.3 67.8 67.9; 64.0 65.1 67.8 68.2 69.1; 65.5 66.0 67.9 68.9 70.2]
%% Part B
for k=TempSenVal
mean(TempSenVal);
end
%% Part C
n=length(TempSenVal);
for k=TempSenVal
mean(n);
end

채택된 답변

KSSV
KSSV 2022년 4월 19일
TempSenVal=[58.3 59.2 60.2 63.0 63.3; 62.5 63.8 64.9 65.2 65.8;...
63.2 64.2 65.3 67.8 67.9; 64.0 65.1 67.8 68.2 69.1; 65.5 66.0 67.9 68.9 70.2] ;
[m,n] = size(TempSenVal) ;
M = zeros(1,n) ;
for i = 1:n
M(i) = mean(TempSenVal(:,i)) ;
end
% just use mean
M
M = 1×5
62.7000 63.6600 65.2200 66.6200 67.2600
mean(TempSenVal)
ans = 1×5
62.7000 63.6600 65.2200 66.6200 67.2600
  댓글 수: 2
Hunter Nau
Hunter Nau 2022년 4월 19일
How do I get the mean for the entire matrix
KSSV
KSSV 2022년 4월 19일
mean(TempSenVal(:))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by