Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with my implementation of logic in this MATLAB code?

Tags:

matlab

I am stuck on a problem with my MATLAB code. This should behave as a pathing program for a calculation. However, it seems to not follow the logic I instructed it. The logic is that, at the current position given, look up, right, down, and left for conditions: (1) It must not be a whitespace " ". (2) It must not go back to its past position. The program behaves well until for position (3, 6) or A(3,6) which it throws me the error code of:

Output argument "new_position" (and possibly others) not assigned a value in the execution with "test>pathfinder" function.

Error in test (line 57)
    [last_direction, new_position] = pathfinder(last_direction, A, current_position);
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I've reviewed the code many times already, and even revised it for good, but the same misbehavior or error still goes. The program is expected that from the source 'S' it will path find any values that is not a whitespace(' ') and is not the direction in which it came from until it gets back to the source's position.

However, as said, at position (3,6) or A(3, 6), the logic seems to misbehave and instead of following my expected behavior, error is thrown. If it helps, the idea was that it can be any array of any design that the program can be capable of pathfinding. The current array included in the code is just for testing. The code is down below:

A = [' ', ' ', ' ', ' ', ' ', ' ', ' ';
     ' ', '-', '-', '2', '-', '-', ' ';
     ' ', 'S', ' ', ' ', ' ', '2', ' ';
     ' ', '-', '-', '-', '-', '-', ' ';
     ' ', ' ', ' ', ' ', ' ', ' ', ' '];

function source_position = source_finder(array, source_symbol)
    [row, col] = find(array == source_symbol);
    source_position = [row, col];

end

function [last_direction, new_position] = pathfinder(last_direction, array, current_position)
    
    row = current_position(1);
    col = current_position(2);

    if (array(row - 1, col) ~= ' ')
        if ~ismember("UP", last_direction)
            new_position = [row - 1, col];
            last_direction = "DOWN";
        else
        end

    elseif (array(row, col + 1) ~= ' ')
        if ~ismember("RIGHT", last_direction)
            new_position = [row, col + 1];
            last_direction = "LEFT";
        else
        end

    elseif (array(row + 1, col) ~= ' ')
        if ~ismember("DOWN", last_direction)
            new_position = [row + 1, col]
            last_direction = "UP"
        else
        end

    elseif (array(row, col - 1) ~= ' ')
        if ~ismember("LEFT", last_direction)
            new_position = [row, col - 1];
            last_direction = "RIGHT";
        else
        end

    end
end

% main

source_position = source_finder(A, 'S');
disp(source_position)
last_direction = "NONE";
current_position = source_position;
while (true)
    
    [last_direction, new_position] = pathfinder(last_direction, A, current_position);
    current_position = new_position;
    disp(current_position)
    disp(last_direction)

    if (current_position == source_position)
        break
    end
end

The overall output for the code run:

     3     2

     2     2

DOWN
     2     3

LEFT
     2     4

LEFT
     2     5

LEFT
     2     6

LEFT
     3     6

UP
Output argument "new_position" (and possibly others) not assigned a value in the execution with "test>pathfinder" function.

Error in test (line 57)
    [last_direction, new_position] = pathfinder(last_direction, A, current_position);
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Where is my logic mistake?

like image 757
William John Otsuro Avatar asked Oct 30 '25 18:10

William John Otsuro


1 Answers

The logic flaw is the following:

if (array(row - 1, col) ~= ' ')
        if ~ismember("UP", last_direction)
            new_position = [row - 1, col];
            last_direction = "DOWN";
        %else   % Why have an empty else???
        end
elseif (array(row, col + 1) ~= ' ')

if (array(row - 1, col) ~= ' ')==TRUE BUT ~ismember("UP", last_direction)==FALSE then this code does never reach the elseif and terminates instead. It gets to the first if, just does nothing inside.

I think you just want to add a return after each last_direction and replace your elseif by if .

Consider using conditional breakpoints to debug your code: MATLAB debugging: smarter way to stop the code with an specific condition?

like image 70
Ander Biguri Avatar answered Nov 04 '25 10:11

Ander Biguri