Max Sum looping error
Show older comments
Hello, I am trying to get the highest value of numbers and can only go up or right in a matrix.
However there is a looping error in lines 16 - 17 that I am trying to figure out. I think its because I don't know how to put in the both the matrix cell and value.
Any help would be appreciated.
Edit: I am making this so much harder then it should be with all the little mistakes and i am so sorry.
a = [7 4 5;8 6 9; 1 2 4 ] ;
[m,n] = size(a) ;
Begin = a(m,1);
End = a(1,n);
disp(Begin)
disp(End)
m0 = m;
n0 = 1;
m1 = 1;
n1 = n;
dot = Begin;
[b] = max(a);
Path = (b);
thesum = 0;
while ((m0~=m1) && (n0~=n1))
if ((m0~= Path) & (n0~= Path))
m0 = m0 - 1;
else
if ((m0 == Path) & (n0 == Path))
n0 = n0 + 1;
else
if ((m0 == m) & (n0 ~=1))
dot = m0 - 1;
else
dot = n0 + 1;
end
end
end
thesum = thesum + a(Begin);
end
disp(thesum)
4 Comments
Geoff Hayes
on 11 Oct 2020
Zachary - please clarify what you mean by the get the highest value of numbers. What does this mean to you?
Zachary Androvich
on 11 Oct 2020
Edited: Zachary Androvich
on 11 Oct 2020
if true
% code
%end
a = [7 4 5;8 6 9; 1 2 4 ] ;
[m,n] = size(a) ;
Begin = a(m,1);
End = a(1,n);
disp(Begin)
disp(End)
m0 = m;
n0 = 1;
m1 = 1;
n1 = n;
dot = Begin;
[b] = max(a);
Path = (b);
thesum = 0;
while ((m0~=m1) && (n0~=n1))
if ((m0~= Path) & (n0~= Path))
m0 = m0 - 1;
else
if ((m0 == Path) & (n0 == Path))
n0 = n0 + 1;
else
if ((m0 == m) & (n0 ~=1))
dot = m0 - 1;
else
dot = n0 + 1;
end
end
end
thesum = thesum + a(Begin);
end
disp(thesum)
1
5
14
What error you are referring to ?. It displays the o/p as above without error
Zachary Androvich
on 11 Oct 2020
Answers (1)
Image Analyst
on 11 Oct 2020
Going from 8 to 6 to 9 is going right, not left.
If you can only go up or left, you will end up at the top row, or the left column and then from then on you can only move along that row or column until you hit the (1,1) element of the matrix. So if you had this matrix
m = [7 4 5
8 6 9
1 2 4]
and you started at the 2, let's look at the two routes:
- If you first moved left, you'd go from 2 to 1, then you can only go upwards so you'd go from 1 to 8 to 7. Now you're at 7 at (1,1) and you can move no further.
- Conversely if you moved up first, then you'd go from 2 to 6 to 4. Then you'd have to move left so you'd go from 4 to 7.
So the solution to find the max in case 1 and case 2 is, if you're starting at (row, col)
row = 3;
col = 2;
m = [7 4 5
8 6 9
1 2 4]
v1 = [m(row, col:-1:1), m(row-1:-1:1, 1)']
max1 = max(v1)
v2 = [m(row:-1:1, col)', m(1, col-1:-1:1)]
max2 = max(v1)
% If you want the overall max covering both routes, you can do this:
overallMax = max([max1, max2])
This is what you'll see:
m =
7 4 5
8 6 9
1 2 4
v1 =
2 1 8 7
max1 =
8
v2 =
2 6 4 7
max2 =
8
overallMax =
8
I don't see how you could get 29 if all you look at are the max values you encounter as you move up or left ONLY and in no other directions.
7 Comments
Image Analyst
on 11 Oct 2020
Note: this post is the newer post. There is a similar post that he posted 6 hours earlier, but I assume this is the latest and greatest.
Zachary Androvich
on 11 Oct 2020
Zachary Androvich
on 11 Oct 2020
Image Analyst
on 11 Oct 2020
Is my assumption of moving ONLY up or to the left correct? So you can't go back to the right side of the row above once you hit the left side (like a raster scan)?
Zachary Androvich
on 11 Oct 2020
Zachary Androvich
on 11 Oct 2020
Image Analyst
on 11 Oct 2020
Are you trying to find the one path, out of all possible paths, with the greatest sum of elements on it? See Steve's blog:
Categories
Find more on MATLAB 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!