フィルターのクリア

How to multiply each element separately from row first to next row in matrix?

7 ビュー (過去 30 日間)
Emilia
Emilia 2018 年 5 月 30 日
コメント済み: Emilia 2018 年 5 月 30 日
Hello,
I want to multiply elements in the first row to the next row within the matrix to get a vector ( without Prod() function , only use of loop)
Example,
% code
matrix [1,2,3;
4,5,6;
7,8,9]
v=[(1*4*7),(2*5*8),(3*6*9)]
I have an error code
% code
a=input('Enter a matrix');
z=[]
[m,n]=size(a);
for i=1:m
c=a(i,:)
c2=a(i+1,:)
d=c.*c2
z=[z,d]
end
Thanks in advance :)

回答 (2 件)

KSSV
KSSV 2018 年 5 月 30 日
編集済み: KSSV 2018 年 5 月 30 日
a=input('Enter a matrix');
[m,n]=size(a);
iwant = zeros(1,n) ;
for j=1:n
P = 1 ;
for i = 1:m
P = a(i,j)*P ;
end
iwant(j) = P ;
end
  2 件のコメント
Emilia
Emilia 2018 年 5 月 30 日
Thank you!
If the matrix is have not nXn, then how do solve this?
KSSV
KSSV 2018 年 5 月 30 日
Modified the code...it shall work for any dimension....:p

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


Stephen23
Stephen23 2018 年 5 月 30 日
編集済み: Stephen23 2018 年 5 月 30 日
Simpler:
M = [1,2,3;4,5,6;7,8,9];
P = 1;
for k = 1:size(M,1)
P = P .* M(k,:);
end
Giving:
>> P
P = 28 80 162
and just for comparison:
>> prod(M,1)
ans = 28 80 162

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by