How to store values from a function file into an array?
4 次查看(过去 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
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!