MATLAB: How to make it a one line code

shortcode

matrix= [1 0 -1 0.001 4; 5.9 -1 3.15 1 1.11 ] ;
cell_conv = (num2cell(matrix));
find_one = abs( matrix ) == 1 ;
cell_conv(find_one)={'hey'}

Best Answer

It is possible to do in one line, but it gets so messy that it is not recommended.
Here is a way to do it with the assistance of a helper function.
IDXAT = @(M,IDX) M(IDX);
disp(IDXAT(cat(3,num2cell(matrix),repmat({'hey'},size(matrix))), reshape(1:numel(matrix), size(matrix))+numel(matrix)*(abs(matrix)==1)))
The output would look like
'hey' [ 0] 'hey' [0.001] [ 4]
[5.9] 'hey' [3.15] 'hey' [1.11]
I am not sure that is acceptable to you.