Write a function called sum_rows

5 ビュー (過去 30 日間)
Siddharth Varshney
Siddharth Varshney 2021 年 12 月 5 日
コメント済み: Stephen23 2021 年 12 月 10 日
Write a function called sum_rows that takes a matrix as input argument
(the function does not have to check the format of the input) and returns
a vector as an output argument. The elements of the vector are the
sums of the elements of the rows of the input matrix. Note that the built-in
MATLAB function sum returns the sum of the columns.
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 5 日
Okay, I have done as you have commanded. What next ?

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

回答 (1 件)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2021 年 12 月 5 日
sum function get dim (dimension which summuation operate on it) as second arguments.
so just use :
sum(A,2)
a function :
function x=sum_(A)
x=sum(A,2);
end
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 5 日
Let us test:
A = [1 2 3; 4 5 6]
A = 2×3
1 2 3 4 5 6
sum_(A)
ans = 2×1
6 15
function x=sum_(A)
x=sum(A,2);
end
Was a vector returned? Yes.
Does the vector equal the sum of the rows? Yes.
Is the function named sum_rows? No.
Is it reasonable to expect that volunteers will give exact solutions to homework questions? No.
Is it reasonable for a volunteer to show the technology the poster needs, assuming that the poster will adapt the ideas and use them as needed? Yes.
John D'Errico
John D'Errico 2021 年 12 月 5 日
編集済み: John D'Errico 2021 年 12 月 5 日
Please do not do student homework assignments for them. This does not help the student, even if they seem grateful. All the student learns is they can use Answers as a way to do their homework for them. I would point out that the poster has posted two obvious homework assignment questions. (I closed the other.) If not discouraged, they will continue to post their entire assignments.

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

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by