Matrix travel by evevation

1 visualización (últimos 30 días)
Kyle Zuvic
Kyle Zuvic el 24 de Feb. de 2020
Editada: Kyle Zuvic el 24 de Feb. de 2020
question following some of the code!
code part 1
function[yesorNo] = iscorner(matrix,row,col)
%shows corners of any size matrix
[numrows, numcols] = size(matrix);
if (row == 1 && col == 1) || (row == 1 && col ==numcols) || (row == numrows && col == 1) || (row == numrows && col == numcols)
yesorNo = true;
else
yesorNo = false;
end
end
code part 2
function [yesorNo] = isedge(matrix, row, col)
%shows edges of any size matrix
[numrows, numcols] = size(matrix);
if (row == 1) || (row == numrows) || (col == 1) || (col == numcols)
yesorNo = true;
else
yesorNo = false;
end
end
code part 3
function [list] = neighbors(matrix, row, col)
%shows neghbors of any matrix location
[numrows, numcols] = size(matrix);
disp("All answers are printed in row, col format");
if (iscorner(matrix,row,col))
%show 3 neighbors
if (row == 1 && col == 1)
list = zeros(3,2);
list(1,:) = [2,1];
list(2,:) = [1,2]; %row,col
list(3,:) = [2,2];
elseif(row == 1 && col == numcols)
list = zeros(3,2);
list(1,:) = [2,numcols-1];
list(2,:) = [2,numcols];
list(3,:) = [1,numcols-1];
elseif(row == numrows && col == 1)
list = zeros(3,2);
list(1,:) = [numrows,2];
list(2,:) = [numrows-1,1];
list(3,:) = [numrows-1,2];
elseif(row == numrows && col == numcols)
list = zeros(3,2);
list(1,:) = [numrows,numcols];
list(2,:) = [numrows-1,numcols-1];
list(3,:) = [numrows-1,numcols-1];
end
elseif (isedge(matrix,row,col) && ~iscorner(matrix,row,col))
%show 5 neightbors
if (row == 1) %Bottom
list = zeros(5,2);
list(1,:) = [row, col - 1]; %row,col
list(2,:) = [row + 1, col - 1];
list(3,:) = [row + 1, col];
list(4,:) = [row + 1, col + 1];
list(5,:) = [row, col + 1];
elseif(row == numrows) %TOP
list = zeros(5,2);
list(1,:) = [row - 1,col - 1];
list(2,:) = [row - 1,col + 1];
list(3,:) = [row - 1,col];
list(4,:) = [row, col - 1];
list(5,:) = [row, col + 1];
elseif(col == 1) %far left side
list = zeros(5,2);
list(1,:) = [row - 1, col];
list(2,:) = [row - 1, col + 1];
list(3,:) = [row, col + 1];
list(4,:) = [row + 1, col];
list(5,:) = [row + 1, col + 1];
elseif(col == numcols) %far right side
list = zeros(5,2);
list(1,:) = [row - 1, col];
list(2,:) = [row - 1, col - 1];
list(3,:) = [row, col - 1];
list(4,:) = [row + 1, col];
list(5,:) = [row + 1, col - 1];
end
elseif (row ~= 1 && row ~= numrows && col ~=1 && col ~= numcols) %Center
%show 8 neighbors
list = zeros(8,2);
list(1,:) = [row, col - 1]; %row,col
list(2,:) = [row, col + 1];
list(3,:) = [row - 1, col - 1];
list(4,:) = [row - 1, col];
list(5,:) = [row - 1, col + 1];
list(6,:) = [row + 1, col - 1];
list(7,:) = [row + 1, col];
list(8,:) = [row + 1, col + 1];
end
end
%THIS IS THE FILE IM WORKING ON NOW
%TO CREATE
%JUST TRYING IDEAS, NOTHING SOLID
I have matrix "mymat" like this:
mymat=
82 28 96 80 68 71
91 55 49 96 76 4
13 96 81 66 75 28
92 97 15 4 40 5
64 16 43 85 66 10
10 98 92 94 18 83
Then "neighbors" creates a matrix that decribe list of the coordinates is like this:
>>neighbors(mymat,3,3)
All answers are printed in row, col format
3 2
3 4
2 2
2 3
2 4
4 2
4 3
4 4
So, the coordinate we can take from matrix "neighbors" above is (3,2), (3,4), (2,2),etc.
Then I want to produce the vector "indexer" that consist of the value of matrix mymat which location is taken from matrix neighbors.
So the vector neighbors that will be produced is like this:
indexer =
96
66
55
49
96
97
15
4
Then after this i need to find a way to subtract each of these "indexer" values from the "neighbor"(origin of that location) to find a gradient, then i must find the smallest gradient (going downhill, so negative) as the output for this function.
In general gist, i am using a location to try to say find the least gradient around me and move there then repeat this over and over until we find the end of the matrix(but only if the got to the lowest location, if not we must back up a step or two and try again but this time disregard the previous attempt)
function [output] = gradient(matrix,row,col)
%takes locations of neighbors and replaces them with values
%then find which value is the lowest slope
%mark this location and move to that one
(neighbors(matrix,row,col))
%index in one location and the input location and find the gradient
indexer=mymat(sub2ind(size(mymat,neighbors(:,row),neighbors(:,col)));
%googled and this was what i got
point(row,col);
indexer(neighbors(row,col));
%number (row,col) - vector(neighbors)
equation = indexer - point;
min(deltas);
output;
end

Respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by