create a 2-column matrix, random set of weight

32 ビュー (過去 30 日間)
형석 김
형석 김 2020 年 9 月 21 日
コメント済み: S. Walter 2020 年 9 月 21 日
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
  1. did i created 2-column matrix correctly? like this? [kg',pound']
  2. and should i create random set of weights with a+(b)rand(1,2) ?

採用された回答

S. Walter
S. Walter 2020 年 9 月 21 日
To answer your questions:
  1. Did you create the column-matrix correctly? Sure. It works :). There are many ways to create matricies. The way you have done it is an acceptable way. A few other ways to make a vector:
kg = linspace(1,2,2) % This makes a 1x2 matrix.
% To convert it to a vector, you would still have to use the apostrophe (')
kg = 1 : 1 : 2; % This makes a 1x2 matrix.
% To convert it to a vector, you would still have to use the apostrophe (')
kg = [1;2]; % This makes a 2x1 matrix. YOU DO NOT HAVE TO USE AN APOSTROPHE (')!
2. Should you create a random set of weights with a+(b)*rand(1,2)? Sure! Remember that you will have to declare your variables a and b but that will work. This will give you a 1x2 matrix again, if you wanted to start with a 2x1 matrix (so you don't have to transpose with ', you can just use rand(2,1)).

その他の回答 (2 件)

BOB MATHEW SYJI
BOB MATHEW SYJI 2020 年 9 月 21 日
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 件のコメント
형석 김
형석 김 2020 年 9 月 21 日
編集済み: 형석 김 2020 年 9 月 21 日
I think both works. i typed it in matlab and both works.
pound=kg.*2.205 and pound=kg*2.205
i've read book and i heard both works. But, originally .* is correct. I'll try to reply in a few days when we must use .* not * . (just to make sure.)
Thanks for responding!
S. Walter
S. Walter 2020 年 9 月 21 日
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
David Hill 2020 年 9 月 21 日
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];

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by