Max Sum looping error

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

Zachary - please clarify what you mean by the get the highest value of numbers. What does this mean to you?
Zachary Androvich
Zachary Androvich on 11 Oct 2020
Edited: Zachary Androvich on 11 Oct 2020
I knew I was bad at explaining this haha...
so in this example
[7 4 5
8 6 9
1 2 4]
I want it to go from 1, to 8, to 6, to 9, and then 5. But it keeps looping the first statement. The reason im using n and m is because I want it to be accessible to other matrixes if i change it.
Edit: nvr mind im stupid its just looping the while loop, I had a breakpoint from me trying to catch the problem but no luck there.
Edit Edit: I am actively trying to solve the problem as I go, should i be updating this post or leaving it in answers as I come across a differnet problem that is still related to it?
VBBV
VBBV on 11 Oct 2020
Edited: VBBV 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
I edited the post so that dot,begin and end aren't involved in the loop at all.
And i've been fiddling with it for awhile now.
Its suppose to spit out 29.

Sign in to comment.

Answers (1)

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:
  1. 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.
  2. 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

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.
I am stupid, its supposed to be right. I am just stupid and need to sleep.
I like to think so. Different problem problem diffrerent post I had assumed and hopefully I can finish it soon, I am new to this site and so I am sorry if this against one of the rules or guidelines.
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)?
no, you can only move to the up and to the right, your suppose to make your way from the bottom left to top right.And try and get a high number. So in this case 1 + 8 + 6 + 9 + 5, or 3,1 to 1,2 to 2,2 to 2,3 to 1,3.
I need to sleep so i'll see you in a couple of hours. I am currently getting my lefts and rights mixed apparently and so should probably not be working code where i can make more messes goodnight and thanks for the help you've already provided.
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:

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 11 Oct 2020

Commented:

on 11 Oct 2020

Community Treasure Hunt

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

Start Hunting!