Max Sum looping error

2 Ansichten (letzte 30 Tage)
Zachary Androvich
Zachary Androvich am 11 Okt. 2020
Kommentiert: Image Analyst am 11 Okt. 2020
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 Kommentare
VBBV
VBBV am 11 Okt. 2020
Bearbeitet: VBBV am 11 Okt. 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
Zachary Androvich am 11 Okt. 2020
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 11 Okt. 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:
  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 Kommentare
Zachary Androvich
Zachary Androvich am 11 Okt. 2020
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.
Image Analyst
Image Analyst am 11 Okt. 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:

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by