Subtracting in matrix, column to column (Basic MatLab)?

Given a 45×48 matrix, type a single command that subtracts column 36 from column 44
i answered this but it pops up as Error: Invalid use of operator. Is there something wrong or should it be in different type of form.
(:,36)-(:,44)

 採用された回答

Star Strider
Star Strider 2019 年 8 月 11 日

0 投票

Your idea is essentially correct. I have no idea what the question wants.
Perhaps providing a matrix and a reference to it will work:
A = rand(45,48);
col_dif = A(:,36)-A(:,44)

6 件のコメント

John wick
John wick 2019 年 8 月 11 日
This is the question "Given a 45×48 matrix, type a single command that subtracts column 36 from column 44"
It's told to be in 1 command but when i try it was an error
col_dif = A(:,36)-A(:,44)
Is this how we need to write? We do need to have the = and A right?
Thank you very much~
Star Strider
Star Strider 2019 年 8 月 11 日
... subtracts column 36 from column 44 ...
I missed the obvious!
Try this instead:
col_dif = A(:,44)-A(:,36)
John wick
John wick 2019 年 8 月 11 日
Why did you swap the 44 and 36?
Star Strider
Star Strider 2019 年 8 月 11 日
Because the instruction is that the operation ‘... subtracts column 36 from column 44 ...’, and
col_dif = A(:,44)-A(:,36)
is the correct operation to subtract column 36 from column 44.
The reference to column 44 must come first in the order of operations, then the subtraction operation and then the value to be subtracted from column 44, here column 36. (There are other ways to code that subtraction and still get the correct result, however this is the most efficient.)
John wick
John wick 2019 年 8 月 11 日
Ohhh Thank you very much for helping and explaining it. Love ya!
Star Strider
Star Strider 2019 年 8 月 11 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2019 年 8 月 11 日
編集済み: Image Analyst 2019 年 8 月 11 日

0 投票

You need to put the matrix name in there, and a result variables, like
result = m(:,44) - .....................
etc.

3 件のコメント

John wick
John wick 2019 年 8 月 11 日
Because the given question is "Given a 45×48 matrix, type a single command that subtracts column 36 from column 44" and nothing else. it kind of make me confuse of what exactly I need to write. Thank youu
Image Analyst
Image Analyst 2019 年 8 月 11 日
Well you can't just have indexes within parentheses like (:,44), because how could MATLAB possibly know which matrix you're referring to.
John wick
John wick 2019 年 8 月 11 日
I guess i just got the idea on how it works but not really master it. Thank you for helping me out!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2019 年 8 月 11 日

コメント済み:

2019 年 8 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by