Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Mischa Kim
Mischa Kim il 11 Mar 2014
Modificato: Mischa Kim il 11 Mar 2014
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.

Più risposte (1)

Md. Tanjin Amin
Md. Tanjin Amin il 8 Lug 2017
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)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by