MATLAB: Compare cell array elements with non zero elements in 2D array

cell arrays

hey i have cell array containing multiple elements e.g.
x{1,1}={2;1}
x{2,1}={1;[1;2]}
and a 2D array:
y=[0,0,-1,1,0,0;-1,0,1,-1,0,0]
for x{1,1}{1,1}=2, it will consider 1st non zero column value (e.g. 3 in 1st row is first non zero column index ) in 1st row of y, and in 2nd row(As x{1,1}{1,1}=2), it will check what value is placed at that non zero index and store it in another cell array. for x{1,1}{2,1} it will consider 2nd non zero element of 1st row.
for x{2,1}{1,1}=1, it will consider 2nd row and check which 1st non zero element.

Best Answer

I assume you've made a mistake with your example result. How can you have 0 in the outptu if you only look at the non-zero values. Also, the first non-zero value in row 1 is -1, so I assume that result{1}{2} should be -1.
If so:
result = cell(size(x));
for row = 1:size(x, 1)
ynonzeros = nonzeros(y(row, :));
result{row} = cellfun(@(cols) ynonzeros(cols), x{row}, 'UniformOutput', false);
end
Because of the need of the temporary ynonzeros it's not possible to replace the outer loop with an arrayfun with an anonymous function, as I had in my previous answer.