Help max sum path of matrix?
Show older comments
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
madhan ravi
on 11 Oct 2020
Expected result?
Zachary Androvich
on 11 Oct 2020
Edited: Walter Roberson
on 11 Oct 2020
madhan ravi
on 11 Oct 2020
If you have what you wanted then what is your question?
Zachary Androvich
on 11 Oct 2020
Edited: Zachary Androvich
on 11 Oct 2020
Answers (1)
KSSV
on 11 Oct 2020
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
on 11 Oct 2020
Edited: Zachary Androvich
on 11 Oct 2020
Categories
Find more on Logical 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!