필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Phoebe
Phoebe 2014년 3월 11일
답변: Md. Tanjin Amin 2017년 7월 8일
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