Product of each row with for-loop
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Azzi Abdelmalek
on 8 Nov 2014
Edited: Azzi Abdelmalek
on 8 Nov 2014
v = [1 2 3; 4 5 6];
out=prod(v,2)
6 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!