# Dividing a Matrix with a column vector (Inside a Loop)

2 views (last 30 days)
Lorenzo Pinna on 29 Jan 2021
Answered: Cris LaPierre on 29 Jan 2021
Hello everyone.
I am having a problem doing a matrix by vector division. The matrices and vectors are here attached.
I am in a loop
for i=1:N N=10000
for t=1:T T=4
and q_i is (TXN), p_i is (TXN) and P is (1XT).
The exact operation is q_i(t,i)=p_i(t,i)./P(t).
Doing this operation the outcome is the matrix q_i attached above.
The problem is that I don't want that the matrix q_i has the same numbers in each cell. What I want is that each element in the row of the matrix p_i is divided by the P element in the relative row(e.g: The first element of P divides all the 10000 columns of p_i in the first row by itself) . If you do this operation not of in a loop, the result would be correct. Giving a matrix q_i that has the same values within the column and different between columns(As the matrix p_i). I would like to obtain the matrix q_i's with cells equal within the column and different between.
Thanks for the availability

Cris LaPierre on 29 Jan 2021
First, the syntax of your for loops is incorrect. Declare N and T before writing the for loops. See the examples on this page.
N=10000;
T=4;
for i=1:N
for t=1:T
end
end
Perhaps more is going on in your code than what you've shared? I get the same result whether I use for loops or elementwise array division. However, the values I get do not match what you have in the q_i variable you have shared.
% manual calculation
p_i(1,1)/P(1)
ans = 6.0098
% for loop result
myq_i(1,1)
ans = 6.0098
q_i(1,1)
ans = 6.3096