PingP
PingP

Reputation: 17

How to create a matrix with the increments within a loop in matlab?

I have created one loop inside another in matlab, and i want to create a matrix inside this second loop that gives the values of the two increments plus a parameter that is being calculated. I made the following code but the matrix is just saving the last values, so it is not a matrix is a vector:

for inclin=29:1:39
    for alfa=1:1:90
        Ii_perc=...
        Di_perc=...
        Gi_perc=...
        r=...
        matriz=[inclin alfa r]
    end
end

So, i want to have a matrix with the different combinations of inclin/alfa/r that the loop gives in each loop, i.e, something like this:

 matriz =[29  1  0.34
          29  2  0.32
          29  3  0.40
          ...........]

I really need some help to solve this problem.. Thanks!

Upvotes: 0

Views: 800

Answers (2)

Guilty Spark
Guilty Spark

Reputation: 89

The problem is that

matriz=[inclin alfa r] 

is a vector. If you want to append an additional row every loop iteration, you need to index it like this:

matriz(i, :)=[inclin alfa r]

Using the colon in this way says to assign the right hand side of the equation to the ith row of matriz.

Upvotes: 0

user4651282
user4651282

Reputation:

If I correctly you understand I can offer this variation:

Matrix = zeros((39-29+1)*90,3);
count = 1;
for inclin=29:1:39
    for alfa=1:1:90
        r=rand();
        Matrix(count,:)=[inclin alfa r];
        count = count+1;
    end
end

Upvotes: 1

Related Questions