It’s a bit unclear to me from the description how you’re getting this output. I’m assuming that you are facing an issue with manipulating the matrices.
Start with loading the values into variables. Use the relational operator to identify the indices in the cost matrix that don’t satisfy the threshold condition –
matValues = ;
loc = cost<20;
After this step, you may use simple if-else statements and indexing through the matrices to get your expected values.
Hope this helps!