Hi, I got a 6by6 matrix and I have to calculate the product in each row like in v = [1 2 3; 4 5 6], the product of row 1 should be 6 and row 2 should be 120. Here is my code, did I miss something?
v = magic(6)
save ('vec.mat')
load ('vec.mat')
[rows, columns] = size(v);
rowpro=1;
for row = 1 : rows;
for i = 1:row
rowpro = rowpro*i;
end
fprintf('The product of row %d is %d \n',row,rowpro)
end
Thanks

Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 8 Nov. 2014
Bearbeitet: Azzi Abdelmalek am 8 Nov. 2014

0 Stimmen

v = [1 2 3; 4 5 6];
out=prod(v,2)

6 Kommentare

david Chan
david Chan am 8 Nov. 2014
how about if I have to use the loop function? thank you for answering anyway
rowpro = rowpro*v(row,i);
v = magic(6)
save ('vec.mat')
load ('vec.mat')
[rows, columns] = size(v);
rowpro=1;
for row = 1 : rows;
for i = 1:row
rowpro = rowpro*v(row,i);
end
fprintf('The product of row %d is %d \n',row,rowpro)
end
here's what I got, however, you product values are wrong
Azzi Abdelmalek
Azzi Abdelmalek am 8 Nov. 2014
Bearbeitet: Azzi Abdelmalek am 9 Nov. 2014
move the initialization rowpro=1; also, look at your for loop counter
david Chan
david Chan am 9 Nov. 2014
if I replaced it, it would said undefined variable.
Azzi Abdelmalek
Azzi Abdelmalek am 9 Nov. 2014
Sorry, I mean change the place

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 8 Nov. 2014

Bearbeitet:

am 9 Nov. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by