フィルターのクリア

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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by