Effacer les filtres
Effacer les filtres

cumulative average for each element

34 vues (au cours des 30 derniers jours)
Ariela Glikman
Ariela Glikman le 26 Nov 2018
Commenté : DGM le 1 Déc 2022
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

Réponse acceptée

madhan ravi
madhan ravi le 26 Nov 2018
Modifié(e) : madhan ravi le 26 Nov 2018
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
>>

Plus de réponses (1)

fakhri
fakhri le 30 Nov 2022
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 commentaire
DGM
DGM le 1 Déc 2022
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"

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by