How do I get my matrix dimensions correct?

3 views (last 30 days)
J0nker
J0nker on 15 Oct 2019
Commented: Guillaume on 15 Oct 2019
When I try to run my code, I get an error at the 'ttank2' line: Matrix dimensions must agree.
I think the problem lies with the matrices of the variables, but i can't find a solution.
L = 400;
%Uniform distribution for each of the variables
pdtank = makedist('uniform','Lower',35,'Upper',45);
pdcombined = makedist('uniform','Lower',75,'Upper',85);
pdtrailer = makedist('uniform','Lower',95,'Upper',105);
pdloading = makedist('uniform','Lower',(1/24),'Upper',(3/24));
pdunloading = makedist('uniform','Lower',(1/24),'Upper',(3/24));
v_tank = random(pdtank,1000,1);
v_trailertank = random(pdcombined,1000,1);
v_trailer = random(pdtrailer,1000,1);
t_loading = random(pdloading,1000,1);
t_unloading = random(pdunloading,1000,1);
x = 1:0.5:50;
y = (100:0.5:150)';
ttank1 = (t_loading + (x./v_trailertank) + t_unloading) + ((L-x)./v_tank);
ttank2 = (t_loading + (x./v_trailertank) + t_unloading) + (x./v_trailer) + (t_loading + (y./v_trailertank) + t_unloading) + ((L-y)./v_tank);
ttank3 = (t_loading + (x./v_trailertank) + t_unloading) + (x./v_trailer) + (t_loading + (y./v_trailertank) + t_unloading) + (y./v_trailer) + (t_loading + (L/v_trailertank) + t_unloading);

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 15 Oct 2019
Edited: KALYAN ACHARJYA on 15 Oct 2019
To run the code
In ttank2 line (Issue with last part)
(L-y) size must be equal to 1000x1
As
>> length(L-y)
ans =
101
You are Trying to dot division by v_tank, which size is 1000x1
(L-y)./v_tank>> Not possible
Solution:
Consider y value having length 1000
Code:
L = 400;
%Uniform distribution for each of the variables
pdtank = makedist('uniform','Lower',35,'Upper',45);
pdcombined = makedist('uniform','Lower',75,'Upper',85);
pdtrailer = makedist('uniform','Lower',95,'Upper',105);
pdloading = makedist('uniform','Lower',(1/24),'Upper',(3/24));
pdunloading = makedist('uniform','Lower',(1/24),'Upper',(3/24));
v_tank = random(pdtank,1000,1);
v_trailertank = random(pdcombined,1000,1);
v_trailer = random(pdtrailer,1000,1);
t_loading = random(pdloading,1000,1);
t_unloading = random(pdunloading,1000,1);
x = 1:0.5:50;
y = linspace(100,150,1000)'; % I have changed here, more numbers of steps
ttank1 = (t_loading + (x./v_trailertank) + t_unloading) + ((L-x)./v_tank);
ttank2 = (t_loading + (x./v_trailertank) + t_unloading) + (x./v_trailer) + (t_loading + (y./v_trailertank) + t_unloading) + ((L-y)./v_tank);
ttank3 = (t_loading + (x./v_trailertank) + t_unloading) + (x./v_trailer) + (t_loading + (y./v_trailertank) + t_unloading) + (y./v_trailer) + (t_loading + (L./v_trailertank) + t_unloading);

Guillaume
Guillaume on 15 Oct 2019
t_loading, v_trailertank, and co. (the random variables) are column vectors 1000 rows by 1 columns.
To these you are adding x./v_trailertank. x is a row vector with 99 elements. A memberwise division (./) of a row vector by a column vector results in a matrix, in this case of size 1000x99. Add that to vectors of size 1000x1 and you still have a matrix of size 1000x99
Then you try to add y./v_trailertank. y is a column vector with 101 elements. You can't do a memberwise division between two column vectors of different size, so indeed you get an error. It's unclear what you were trying to do. Note that even if y was a row vector, like x, there would still be a problem since it would result in a 1000x101 matrix which can't be added to a 1000x99 matrix.
While we can explain why there is an error, since you haven't explained what you're trying to do and your code has no comments (hint!) we can't tell you how to solve it other than not trying do operations that don't make sense.
Note: explanation of what happens when you combine row and column vectors: Compatible array sizes.
  2 Comments
J0nker
J0nker on 15 Oct 2019
Thank you for your answer. I understand now why it doens't work, but still can't get it to work properly.
I'll try to explain what I'm trying to do:
For each of the three expressions I'm looking the time for each tank to travel a certain distance L. This can be used to calculate the total travel time. The variables are uniformly distributed instead of fixed, because I'm looking for the expected travel time (mean time of all the samples). That is also why there a so many samples.
It works when the variables are constant, because I don't have any problems with the dimensions.
I hope this makes any sense.
Guillaume
Guillaume on 15 Oct 2019
I'm still not clear what you're trying to do. Possibly you have three orthogonal variables, x, y and the random variables. In that case, you need to work with 3D matrices and the easiest way to do that is to generate these random vectors in the 3rd dimension:
v_tank = random(pdtank, 1 ,1, 1000); %1x1x1000 vector
v_trailertank = random(pdcombined, 1, 1, 1000);
%and so on..
Your expressions don't need to change and will produce 101x99x1000 matrices.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!