how to get sum of numbers appearing in triangle in a matrix

2 ビュー (過去 30 日間)
Ayyaz Ahmed
Ayyaz Ahmed 2016 年 8 月 7 日
コメント済み: Image Analyst 2016 年 8 月 9 日
I want to find sum of numbers appearing in right triangular part from bottom right corner to top left corner in a 2-D matrix like
o = [1 2 3 4;5 6 7 8]
f = [1,2,3,4; 5,6,7,8; 2,3,4,5]
sum of o = 1+5+6+7+8 = 27
sum of f = 1+5+6+2+3+4+5 = 26
  1 件のコメント
Image Analyst
Image Analyst 2016 年 8 月 7 日
It's not clear what you are doing. Do you only have short, wide arrays and you using a lower square triangle and then just copying the entire last row (like Stephen's solution does)? What if the array is taller than it is wide? Does that ever happen to you, and what elements do you sum if that is the case?

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

採用された回答

Image Analyst
Image Analyst 2016 年 8 月 7 日
You need to decide if the row and column are in the lower triangle and if it is, sum it up. This code works for both your (badly-named) "o" and your "f".
o = [1 2 3 4;5 6 7 8]
f = [1,2,3,4; 5,6,7,8; 2,3,4,5]
m = f % or set equal to f.
[rows, columns] = size(m)
deltaX = columns - 1;
deltaY = rows - 1;
slope = deltaY / deltaX
theSum = 0;
for row = 1 : rows
for col = 1 : columns
x = col;
y = row;
yOnSlope = slope*(x-1)+1;
fprintf('row (y) = %d, col (x) = %d, yOnSlope = %.2f', y, x, yOnSlope);
if y >= yOnSlope
% It's in the lower triangle
theSum = theSum + m(row, col);
fprintf('<-- This is in the lower triangle');
end
fprintf('\n');
end
end
% Echo to command window.
theSum
In the command window:
row (y) = 1, col (x) = 1, yOnSlope = 1.00<-- This is in the lower triangle
row (y) = 1, col (x) = 2, yOnSlope = 1.33
row (y) = 1, col (x) = 3, yOnSlope = 1.67
row (y) = 1, col (x) = 4, yOnSlope = 2.00
row (y) = 2, col (x) = 1, yOnSlope = 1.00<-- This is in the lower triangle
row (y) = 2, col (x) = 2, yOnSlope = 1.33<-- This is in the lower triangle
row (y) = 2, col (x) = 3, yOnSlope = 1.67<-- This is in the lower triangle
row (y) = 2, col (x) = 4, yOnSlope = 2.00<-- This is in the lower triangle
theSum =
27
and for "f":
row (y) = 1, col (x) = 1, yOnSlope = 1.00<-- This is in the lower triangle
row (y) = 1, col (x) = 2, yOnSlope = 1.67
row (y) = 1, col (x) = 3, yOnSlope = 2.33
row (y) = 1, col (x) = 4, yOnSlope = 3.00
row (y) = 2, col (x) = 1, yOnSlope = 1.00<-- This is in the lower triangle
row (y) = 2, col (x) = 2, yOnSlope = 1.67<-- This is in the lower triangle
row (y) = 2, col (x) = 3, yOnSlope = 2.33
row (y) = 2, col (x) = 4, yOnSlope = 3.00
row (y) = 3, col (x) = 1, yOnSlope = 1.00<-- This is in the lower triangle
row (y) = 3, col (x) = 2, yOnSlope = 1.67<-- This is in the lower triangle
row (y) = 3, col (x) = 3, yOnSlope = 2.33<-- This is in the lower triangle
row (y) = 3, col (x) = 4, yOnSlope = 3.00<-- This is in the lower triangle
theSum =
26
  2 件のコメント
Ayyaz Ahmed
Ayyaz Ahmed 2016 年 8 月 9 日
your code working correctly.Thanks a lot.but i could not under how you used slope method?can u explain it.
Image Analyst
Image Analyst 2016 年 8 月 9 日
I basically drew a line from one corner to the other. This line will not necessarily fall exactly at the center of the matrix element - the line will probably go above or below the exact center. So I just find out the y value (the row) that the line has when it's at that x (column). If the element row is below that, it's in the lower triangle, if the element row is above the line, it's in the upper triangle. For example if, say, in column 5 the line would have a value of 3.1234, then elements in column 5 that are in rows 1, 2, and 3 are above the line (because they're less than 3.1234), and rows 4, 5, 6, etc. are below the line (because they are greater than 3.1234).

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

その他の回答 (1 件)

Stephen23
Stephen23 2016 年 8 月 7 日
編集済み: Stephen23 2016 年 8 月 7 日
The simplest solution is the best:
>> f = [1 2 3 4; 5 6 7 8; 2 3 4 5]
f =
1 2 3 4
5 6 7 8
2 3 4 5
>> X = tril(f);
>> X(end,:) = f(end,:)
X =
1 0 0 0
5 6 0 0
2 3 4 5
>> sum(X(:))
ans =
26
  3 件のコメント
Stephen23
Stephen23 2016 年 8 月 7 日
@Image Analyst: it must be an off day for you, do you really imagine that I did not test this?
>> f = [1 2 3 4; 5 6 7 8]
f =
1 2 3 4
5 6 7 8
>> X = tril(f);
>> X(end,:) = f(end,:)
X =
1 0 0 0
5 6 7 8
>> sum(X(:))
ans =
27
Image Analyst
Image Analyst 2016 年 8 月 7 日
編集済み: Image Analyst 2016 年 8 月 7 日
Sorry, you're right - I must have forgot to change one letter when I was changing your code from f to o. Sometimes, I post code off the top of my head without testing, though I know now you don't do that. +1 vote.

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

カテゴリ

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

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by