フィルターのクリア

cumulative average for each element

34 ビュー (過去 30 日間)
Ariela Glikman
Ariela Glikman 2018 年 11 月 26 日
コメント済み: DGM 2022 年 12 月 1 日
hey, i need to build a function who receives a vector and return the cumulative average for each element . for ex: vecMean([1 2 3 4])= [1 1.5 2 2.5].
the problem is that im reciving a row vector but sometimes i want to put column vec and recive a column also.
% the function calculates the cumulative average of elements in a vector.
% each element in the output vector is the average of this element and all
% previous elements in the input.
% the function gets a vector of numbers (=vecOfNum)
% and return output vector (=vecPrevMean),
% each element is the average of all previous elements.
function [vecPrevMean]= vecMean(vecOfNum)
count=1; %count the amount of elements in the vector
sumNum=0; %sum the elements
for i=1:length(vecOfNum);
if iscolumn(vecOfNum)==0
sumNum= sumNum+ vecOfNum(i);
vecPrevMean(i)=sumNum./count;
count=count+1;
elseif iscolumn(vecOfNum)==1
sumNum= sumNum+ vecOfNum(i);
vecPrevMean(i)=(sumNum./count)';
count=count+1;
end
end

採用された回答

madhan ravi
madhan ravi 2018 年 11 月 26 日
編集済み: madhan ravi 2018 年 11 月 26 日
no loops needed:
>> a=1:4
desired_result=cumsum(a)./(1:numel(a))
a =
1 2 3 4
desired_result =
1.0000 1.5000 2.0000 2.5000
>>

その他の回答 (1 件)

fakhri
fakhri 2022 年 11 月 30 日
hey,
Write a function called matMean that calculates the cumulative average of the rows or the columns of a matrix. Your function should get two input arguments: • First input argument: a matrix of numbers. • Second input argument: Type of cumulative average to calculate - can get one of two values: the number 1 for calculating the cumulative average of the rows of the matrix, or the number 2 for calculating the cumulative average of the columns of the matrix.
For this question you should write your own algorithm for calculating the cumulative averages of the arrays - do not use built-in statistical MATLAB functions such as: mean, sum, cumsum, etc
  1 件のコメント
DGM
DGM 2022 年 12 月 1 日
What part of this is an answer to the question?
What have you done other than pasting your assignment?
" For this question you should write your own algorithm"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by