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

2 Ansichten (letzte 30 Tage)
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 Kommentar
Image Analyst
Image Analyst am 7 Aug. 2016
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 7 Aug. 2016
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 Kommentare
Ayyaz Ahmed
Ayyaz Ahmed am 9 Aug. 2016
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 am 9 Aug. 2016
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).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 7 Aug. 2016
Bearbeitet: Stephen23 am 7 Aug. 2016
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 Kommentare
Stephen23
Stephen23 am 7 Aug. 2016
@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 am 7 Aug. 2016
Bearbeitet: Image Analyst am 7 Aug. 2016
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by