user3464737
user3464737

Reputation: 1

Saving data from for loop in an array matlab

I'm trying to calculate an array but my for loop produces the numbers individually and then saves the last output as the vector can you please help? My code is:

for k= 0:0.1:3
muk = [nanmean(abs(normGsf).^(k))] 
end

The output I am seening is

muk =

 1

muk =

0.9169

muk =

0.8520

muk =

0.8011

muk =

0.7616

muk =

0.7314

muk =

0.7089

muk =

0.6932

muk =

0.6836

muk =

0.6794

muk =

0.6805

muk =

0.6866

muk =

0.6976

muk =

0.7138

muk =

0.7351

muk =

0.7621

muk =

0.7950

muk =

0.8345

muk =

0.8812

muk =

0.9359

muk =

0.9996

muk =

1.0736

muk =

1.1592

muk =

1.2582

muk =

1.3724

muk =

1.5043

muk =

1.6567

muk =

1.8327

muk =

2.0362

muk =

2.2719

muk =

2.5450

Upvotes: 0

Views: 76

Answers (2)

Steve Osborne
Steve Osborne

Reputation: 680

You could do something like this:

muk = [];    
for k= 0:0.1:3
muk = [muk; nanmean(abs(normGsf).^(k))] 
end

This appends the new values to "muk" each time through the loop. Note that this is not necessarily a good practice if the number of loop iterations is really large, but you are far, far from that limit.

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112659

Why not vectorize?

k = 0:0.1:3;
muk = nanmean(abs(normGsf)).^k;

I'm assuming nanmean(abs(normGsf)) is a scalar, as in your example. Then the result muk is a vector with the same size as k.

Upvotes: 1

Related Questions