create a 2-column matrix, random set of weight
Show older comments
Hello here is one question i have. ( my english is bad. pardon me.)
"create a 2-column matrix kg-pound, where you convert a random set of weights in kilograms to pounds."
i've thought this would be correct.
>>kg=[1,2];
>>pound=kg*2.205;
>>[kg',pound,']
ans=
1.0000 30.4800
2.000 60.9600
- did i created 2-column matrix correctly? like this? [kg',pound']
- and should i create random set of weights with a+(b)rand(1,2) ?
Accepted Answer
More Answers (2)
BOB MATHEW SYJI
on 21 Sep 2020
To multiply each element of the matrix kg, you have to use .* instead of * I hope this code works. If not please rectify
kg=[1 2];
pound=kg.*2.205;
kg_pound=[kg',pound'];
3 Comments
S. Walter
on 21 Sep 2020
Actually, this is only true if you want to multiply individual entries of two matricies together. If you are multiplying a matrix by a scalar, the whole matrix will be multiplied by that scalar.
When to use dot multiplication (.*)?
Say you have two matricies that represent the state of a system. You have temperatures and pressures for air and want to find the density. We know:
where P is the pressure, rho is the density, R is the specific gas constant (we'll use 287 J/(kg-K) for air), and T is the absolute temperature. Thus, rearranging, density is
You have temperatures ranging from 300K to 500K with pressures ranging from 1 to 9 atmospheres:
% Gas constant
R = 287; % [J/(kg-K)]
% Temperature vector:
T = 300:25:500; % [K]
% Pressure vector:
P = 1:1:9; % [atm]
To make the equation work, you have to convert pressure from atmospheres to Pascal. That can be done by multiplying the pressure vector by 101,325.
P = P*101325; % [Pa]
You can multiply just by the scalar. You could use dot multiplication, but it doesn't change the answer. Now for the density, you can take the pressure in Pascal and you have to divide by temperature. In this case, if you don't use the dot division, you get this:
>> P/(R*T)
ans =
4.6595
It did an actual matrix operation, leaving you with just one entry. If instead you use dot division:
>> P./(R*T)
ans =
Columns 1 through 7
1.1768 2.1726 3.0261 3.7659 4.4131 4.9842 5.4919
Columns 8 through 9
5.9461 6.3549
You now get a 1x9 solution, which is what you want. Note that you don't have to use dot multiplication on the gas constant, R, since that is a scalar.
(Sorry about the long answer, hope it helps though!)
S. Walter
on 21 Sep 2020
You are right, both will work.
But dot multiplication isn't necessary when you are multiplying scalars.
It's always good practice to know when to utlize what form of operation so that you don't end up with undesirable results.
Good luck and have fun coding!
David Hill
on 21 Sep 2020
kg=10*rand(100,1);%however you want to create your numbers depending on your range and other statistics
pound=kg*2.205;
matrix=[kg,pound];
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!