If Most Recent Instance of a Block Contains X in Y Field Then Do Z
My answer is based on the assumption you only have one matrix module you want to compare, otherwise you would have to store your values in an array with the key as block.getType().handle or you would have to create multiple variables.

You want to create a variable that stores the value of your field if the block handle matches. So you check if your handles are equal and compare them

{% set latestBlockValue = null %}
{% for block in entry.matrix %}
    {% if block.getType().handle == 'referenceBlockType' %}
        {% if latestBlockValue is not null and latestBlockValue == x %}
            {# z #}
        {% endif %}
        {% set latestBlockValue = block.y %}
    {% endif %}
{% endfor %}