MATLAB Answers

J0nker
0

How do I get my matrix dimensions correct?

Asked by J0nker
on 15 Oct 2019
Latest activity Commented on by 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);

  0 Comments

Sign in to comment.

Products


Release

R2019a

2 Answers

Answer by KALYAN ACHARJYA on 15 Oct 2019
Edited by 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);

  0 Comments

Sign in to comment.


Answer by 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

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.
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.