Multiplication of each column with each other with a condition

4 ビュー (過去 30 日間)
Sukru Yavuz
Sukru Yavuz 2018 年 3 月 12 日
回答済み: Bob Thompson 2018 年 3 月 12 日
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 件のコメント
Adam
Adam 2018 年 3 月 12 日
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.

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

回答 (1 件)

Bob Thompson
Bob Thompson 2018 年 3 月 12 日
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

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by