how can fill a table using a loop ?

9 visualizaciones (últimos 30 días)
Mohamed ben hadj yahia
Mohamed ben hadj yahia el 20 de Mayo de 2019
Respondida: Mohamed ben hadj yahia el 20 de Mayo de 2019
hello I m having a problem filling a table
let's say I have a table
a c
1
4
10
11
...
n
I want c to be the mean of a starting from 1 to n
for example
c(1) = 1
c(2) =1+4 / 2
c(3) = 1+4+10 / 3
c(4)=1+4+10+11 / 4
... n
how do I fill c using a loop ?
thank you

Respuesta aceptada

Adam Danz
Adam Danz el 20 de Mayo de 2019
Editada: Adam Danz el 20 de Mayo de 2019
No-loop method
There's no need for a loop.
b = cumsum(a)./(1:length(a));
Loop method
If you must use a loop...
b = zeros(size(a));
for i = 1:length(a)
b(i) = sum(a(1:i))/i;
end
If you want that in a table,
t = table(a',b','VariableNames',{'a','b'});

Más respuestas (1)

Mohamed ben hadj yahia
Mohamed ben hadj yahia el 20 de Mayo de 2019
Thank you that was very helpfull

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by