2 views (last 30 days)

Im relatively new to MatLab, and im struggling with the next phase of simulation. My previous Vn was a 5x1 Matrix, and now that I have replaced it with a 5x5, the rule wont work. Anyone have any suggestions on why it isnt working. When I multiplied the matrices out normally it worked fine, but it wont work with the current rule.

Thanks!

T = ones(5);

D = [0.9 0 0 0 0;0 0.9 0 0 0;0 0 0.9 0 0;0 0 0 0.9 0;0 0 0 0 0.9];

C = [0 0 0 0 0;0 0 0.2 0 0;0 0.2 0 0.2 0;0 0 0.2 0 0;0 0 0 0 0];

i = 1;

Vn= zeros(5)

for i=1:95 % i being the number of resultant vectors

Vn(:,i+1) = T - (D*(T-Vn(:,i)) - (C*Vn(:,i))) % colon meaning search in any row, i+1 searching in the next column

for j = 1:5

% j being the next variable after i, j being from the first row to the fifth

if Vn(j,i+1) > 1; % if any value in the jth row, and the next column is above 1, its equal to 1

Vn(j,i+1) = 1;

end

j = j+1; % new j = old j + 1, until its range is complete (Line13)

end

i = i+1; % new i = old i + 1 for the range of i

end

Fabio Freschi
on 3 Mar 2020

Inside the loop in the first instruction Vn(:,i) is 5x1 but T is 5x5. Matlab now (starting in 2016, I guess) implicitly expands Vn(:,i) to fit T, but the result of this expansion is a 5x5 matrix that you want to save in Vn(:,i+1) that is 5x1. Here comes the error. What do you want to do (with paper and pencil)?

Btw: I guess the initialization ov Vn should be zeros(5,95).

Fabio Freschi
on 4 Mar 2020

Vn(:,i) and Vn(:,i+1) are not a 5x5 matrix. It is 5x1. What is exactly what you want to do? Can you make the first calculation with pen and paper? This will clarify a lot the expected result. For example, when you write

T-Vn(:,i)

You are doing

|1 1 1 1 1| |0|

|1 1 1 1 1| |0|

|1 1 1 1 1| - |0|

|1 1 1 1 1| |0|

|1 1 1 1 1| |0|

This operation is not defined in standard algebra. What do you want to do with this?

Fabio Freschi
on 5 Mar 2020

I start a new clean answer

- You don't have to use the index i inside your matrix like you are doing in your code: in that way you are pointing to the column of the matrix Vn. Just leave the index i out.
- it is not clear to me the for-if part. I guess you are setting the matrix value to 1 if any entry is larger than one.

So the code I would use is

T = ones(5);

D = [0.9 0 0 0 0;0 0.9 0 0 0;0 0 0.9 0 0;0 0 0 0.9 0;0 0 0 0 0.9];

C = [0 0 0 0 0;0 0 0.2 0 0;0 0.2 0 0.2 0;0 0 0.2 0 0;0 0 0 0 0];

i = 1;

Vn= zeros(5)

for i=1:95

Vn = T - (D*(T-Vn) - (C*Vn));

Vn(Vn > 1) = 1;

end

In this case the matrix Vn is overwritten at each iteration. The results are identical to those of your picture. It is still not clear to me why the loop is going from 1 to 95, but this is another problem. Let me know if this solves your original problem

Opportunities for recent engineering grads.

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

Start Hunting!
## 0 Comments

Sign in to comment.