How to store values from a function file into an array?

2 次查看(过去 30 天)
If I have the following from a subroutine in my code,
for counter = 1 : diagcounter
[M]=xtMean(Xtcounter, counter);
counter=counter+1;
end
function [ M ] = xtMean(Xtcounter, counter)
for i = 1 : counter
A=Xtcounter(i,:);
mA=mean(A);
a=A(1);
b=A(2);
c=A(3);
d=A(4);
e=A(5);
a1=abs(a-mA);
b1=abs(b-mA);
c1=abs(c-mA);
d1=abs(d-mA);
e1=abs(e-mA);
A1=[a1 b1 c1 d1 e1];
M=sum(A.*A1) / sum(A)
end
end
I know it works as it produces values for example for M as follows,
M =
0.6667
M =
0.800
M =
0.6667
M =
0.7500
I need to store all these output into an array which i will call Xtmean but I am struggling. I have tried things such as within the for counter = 1 : diagcounter loop,
for k = 1 : diagcounter
Xtmean(k)=M;
end
but then it just stores same value like 40 times. Anyway if anyone could help on this it would be MUCH appreciated! Thanks!! Phoebe

采纳的回答

Mischa Kim
Mischa Kim 2014-3-11
编辑:Mischa Kim 2014-3-11
Phoebe, just use
M(i) = sum(A.*A1) / sum(A);
in function xtMean to store values into array M.
The question is, what do you do with the returned array... How do the first and third for-loops fit into all of this? If you need to store each of the M arrays in Xtmean you are probably best of using a cell array, since the size of M seems to be changing.
for counter = 1:diagcounter
Xtmean{counter} = xtMean(Xtcounter, counter);
% counter = counter+1;
end
Notice the curly braces (= cell array). Also, for-loops automatically increment loop indices ( counter ), so I'm not sure if you still need the command I commented out.

更多回答(1 个)

Md. Tanjin Amin
Md. Tanjin Amin 2017-7-8
hi,
i want to write the following equation in a for loop to store values for 1000 rows..Can anyone help please?
z(k)=lamda*x(k)+(1-lamda)*z(k-1)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by