How is it possible to plot the average of a vector that has a different size in each iteration?

I'm trying to obtain a plot of M by averaging 10 simulations of M, but the problem is that it has a different size in each run.
Of course I'm getting this error message:
Unable to perform assignment because the size of the left side is 1-by-17 and the size of the right side is 1-by-15
for jj = 1:10
[G_dmg,G_orig, M,L_fail,overLoad,b] = Load_initial(G,5,0,460,1010,600,1000);
t = 2;
while M(t-1)- M(t)~=0
[G_dmg,M,b] = Load_Stages(G_dmg,L_fail,M,b,25);
t = t+1;
end
Mavg(jj,:)=M;
end
Mavg = mean(Mavg,1);
figure(1)
plot(1:length(Mavg(1:end-1)),Mavg(1:end-1));
Thank you.

 Réponse acceptée

Mavg = zeros(10,1) ;
for jj = 1:10
[G_dmg,G_orig, M,L_fail,overLoad,b] = Load_initial(G,5,0,460,1010,600,1000);
t = 2;
while M(t-1)- M(t)~=0
[G_dmg,M,b] = Load_Stages(G_dmg,L_fail,M,b,25);
t = t+1;
end
Mavg(jj)=mean(M);
end
plot(Mavg)

11 commentaires

Hi KSSV,
Thanks for your reply.
Your code find the average of M in each iteration and assign the result to Mavg. What I'm looking for is to find the 10 different M's and assign the result to Mavg (should be a matrix of size 10 by no. of columns) and then find the average column wise. The problem is that M has a different no. of columns in each one of the 10 iterations.
If you have different dimensions in the matrix i.e. columns..how you can get the mean along the columns?
I know it is impossible to do that but I thought there might be another method to find the average plot of M that I'm not aware of.
Thanks for your time.
You want to introduce NaN's when number of elements are less and use nanmean?
Mavg = cell(10,1) ;
for jj = 1:10
[G_dmg,G_orig, M,L_fail,overLoad,b] = Load_initial(G,5,0,460,1010,600,1000);
t = 2;
while M(t-1)- M(t)~=0
[G_dmg,M,b] = Load_Stages(G_dmg,L_fail,M,b,25);
t = t+1;
end
Mavg{jj}=M;
end
% Make Mavg a matrix adding NaN's
L = max(cellfun(@length,A)) ;
iwant = NaN(L) ;
for i = 1:length(Mavg)
iwant(i,1:length(Mavg{i})) = Mavg{i} ;
end
Mavg = nanmean(iwant) ;
It works for 10 iterations, thank you so much Doctor. But when I increase the no. of iterations to 20 or 30, the plot of Mavg was not as expected.
Did you do the intilalization part, loop indices and all correct?
No I have not. I'm attaching two plots of Mavg for 2 different no. of iterations 10 and 30.
The plot for 10 iterations is perfect but as for 30,the curve should not go down again once it reaches 5000.
So basically, the x-axis represents the time which is also the no. of columns of M and the y-axis represents the cumulative number of packets of information fails in each time step. And the total no. of packets is 5000, so the curve eventually should stop once it reaches 5000.

Connectez-vous pour commenter.

Plus de réponses (1)

Finding the minimum length ( assum it is 10) , then use the moving average ( smoothing) all the other results to get all of them with same length (10). In short, shorten all the arrays to one fixed length by averaging them using smooth function.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by