Help max sum path of matrix?

Ok so how do you do the thing in the title I guess, you start from the bottome left and end at top right, you can only move either to the right or up and you have to try and get the maximum sum you can get from a matrix.
I am just trying to use a preset matrix [7 4 5;8 6 9; 1 2 4 ], before trying to a matrix that people can input for.
What i have so far.
m = input("Input the number of columns that the matrix will have?");
n = input("Input the number of rows that the column will have?");
v = (m:n);
a = [7 4 5; 8 6 9; 1 2 4];
disp (a)
[a,b] = max(a);
disp(a)
disp(b)
disp(max(a,b))
[maxValue, linearIndexesOfMaxes] = max(a(:));
disp(maxValue)
disp(linearIndexesOfMaxes)
disp(v)
I don't know how to do the direction things, how to go on the path for max sum, I've been trying to do the research for this but I am just feelign more and more confused as i go on.

4 Comments

Expected result?
Something like The best path is: Grid (1,1), Grid (xx ,xx), …, Grid(3,3) The maximum number of carrots collected is: xx.
It can be simpler then that.
Also just working and currently what i have.
% Get the number of rows and columns for the matrix
m = input("Input the number of rows that the matrix will have?");
n = input("Input the number of columns that the column will have?");
disp('Starting from top left to bottom right')
% Set up loop that populates the matrix with user inputted numbers.
for i= 1:m
for j = 1:n
a(i,j) = input ('number in matrix ');
end
end
a = reshape(a,m,n);
disp (a)
[a,b] = max(a);
disp(a)
disp(b)
disp(max(a,b))
[maxValue, linearIndexesOfMaxes] = max(a(:));
disp(maxValue)
disp(linearIndexesOfMaxes)
disp(v)
If you have what you wanted then what is your question?
Zachary Androvich
Zachary Androvich on 11 Oct 2020
Edited: Zachary Androvich on 11 Oct 2020
Well it doesn't really work?
Like I'm trying to have a starting point of (1,m) or bottom left and going to (n,1) while pathing in a way to get the maximum value. However it can only go up and right.
I don't know how to do any of the coding related to pathing, and recognising the highest max value path.

Sign in to comment.

Answers (1)

clc; clear all ;
A = [7 4 5;8 6 9; 1 2 4 ] ;
[m,n] = size(A) ;
m0 = 3 ; n0 = 1 ; % Initial position
m1 = 1 ; n1 = 3 ; % final position
thesum = 0 ;
count = 0 ; % number of steps
while ((m0 ~= m1) & (n0~=n1))
if rand > 0.5 % move top
m0 = m0+1 ;
if m0 > m
m0 = m0-1 ;
end
else
n0 = n0+1 ; % move right
if n0 > n
n0 = n0-1 ;
end
end
thesum = thesum+A(m0,n0) ;
count = count+1 ;
end

1 Comment

Zachary Androvich
Zachary Androvich on 11 Oct 2020
Edited: Zachary Androvich on 11 Oct 2020
But isn't the max possible is 29?, with it going 1 then 8 then 6 then 9 then 5?
Edit: why is there a rand?

Sign in to comment.

Products

Release

R2020a

Asked:

on 11 Oct 2020

Edited:

on 11 Oct 2020

Community Treasure Hunt

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

Start Hunting!