How do I get my matrix dimensions correct?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
KALYAN ACHARJYA
le 15 Oct 2019
Modifié(e) : KALYAN ACHARJYA
le 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 commentaires
Guillaume
le 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.
2 commentaires
Guillaume
le 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.
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!