Multiplication of each column with each other with a condition

18 views (last 30 days)
I have a table of 4x11. And I would like to multiply each column with each other and save the results to another table. for example: firstresults = table(1,1)*table(1,2)*table(1,3)...*table(1,11). When columns reach 11, I want my loop to start calculate it for second rows. secondresults = table(2,1)*table(2,2)..table(2,11). I am trying to do that with for loop but I just can't. Is it something like that?
secondresults(ii,j) = table(ii,j) * table(ii,j+1).
if j <= 14,
ii = ii+1 Got stuck for hours. Thanks in advance.
  1 Comment
Adam
Adam on 12 Mar 2018
If you have an array you can just do
results = prod( table, 2 );
to get all the results in a single variable. I don't know off-hand how you would do this if you have an actual table, but if you do then you just overwrote the 'table' function with your variable name too which is not good at all.

Sign in to comment.

Answers (1)

Bob Thompson
Bob Thompson on 12 Mar 2018
You could accomplish this fairly easily using one or two for loops, depending on your style. The only loop you really require is a loop to run through the rows. For the columns, you can include a loop if you would like, or have a variable number of rows, or you can simply define it ahead of time.
for k = 1:size(table,1) % Run through all rows
results(k) = table(k,1)*table(k,2)*...*table(k,11) % This is the manual entry version, you need to enter all the columns individually.
end
for k = 1:size(table,1) % Again, all rows
results(k) = 1; % You need to pre-initialize your results for this one
for h = 1:size(table,2) % Run through all columns
results(k) = results(k)*table(k,j) % Multiply columns of row k together. This is the automated version, but added complexity could potentially produce errors
end
end

Categories

Find more on Debugging and Analysis 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!