# Simulation errors when multiplying matrices

2 views (last 30 days)
Caillin O'Rourke on 3 Mar 2020
Answered: Caillin O'Rourke on 5 Mar 2020
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).

Caillin O'Rourke on 4 Mar 2020
Hi Fabio,
Thanks for taking the time to help me. Im trying to run a simulation that will multiply the next series of matrices, Vn being the starting 5x5. Im trying to run the rule Vn(:,i+1) = T - (D*(T-Vn(:,i)) - (C*Vn(:,i)))
so it will give me the next series of matrices, to whatever range I want, but this rule isnt code isnt working. I understand converting it from a 5x1 to a 5x5 is where the error is, but they are all 5x5 matrices so i dont know where the error in the code actually is.
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?

Caillin O'Rourke on 4 Mar 2020

#### 1 Comment

Caillin O'Rourke on 4 Mar 2020
This is what im trying to achieve, im trying to run a simulation to get all the resulting matrices until it is a 5x5 matrix of ones.

Fabio Freschi on 5 Mar 2020
I start a new clean answer
1. 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.
2. 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

Caillin O'Rourke on 5 Mar 2020
Not quite, the reason I had the index i in the first for loop was to run the series of multiplixations, from 1 to 95, or to however simukations it takes to reach all ones. Notice at the start when i had Vn(:,i + 1), it would give me the next matrix from i. From your above answers, i think the problem is converting the Vn(:.i+1) from a 5x1 matrix to a 5x5 matrix.

R2014a

### Community Treasure Hunt

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

Start Hunting!