フィルターのクリア

Multiplication of column elements of a matrix

2 ビュー (過去 30 日間)
Vinay Killamsetty
Vinay Killamsetty 2020 年 4 月 19 日
コメント済み: Ameer Hamza 2020 年 4 月 24 日
Hi
If I am having a marix of size "m*n"
I have to find the product of column elements of each row (But the number of columns selected for multiplication has to be user selected)
for example :
if the matrix is A=[1 2 3;4 5 6;7 8 9]
I want find the resultant vector containing [1 2*5*7 3*6] (multiplication of 1 element in the ist row, 2 elements in the second row, 3 elements in the 3rd row)
for this is have used the code
A=[1 2 3;4 5 6;7 8 9];
row_elemens=[1 3 2];
for i=1:3
result=prod(A(i,1:row_elemens(i)));
end
is there any simple solution for this problem without using any loop and with a single line

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 19 日
Your question mentioned "without using any loop and with a single line". Try this
A=[1 2 3;4 5 6;7 8 9];
row_elemens=[1 3 2];
result = arrayfun(@(i) prod(A(i,1:row_elemens(i))), 1:3);
  2 件のコメント
Vinay Killamsetty
Vinay Killamsetty 2020 年 4 月 24 日
Thank you very much
Ameer Hamza
Ameer Hamza 2020 年 4 月 24 日
I am glad to be of help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by