Replace all zeros and NaNs in a matrix with the string 'error' - MATLAB Cody - MATLAB Central

Problem 868. Replace all zeros and NaNs in a matrix with the string 'error'

Difficulty:Rate

Given a numeric input matrix A, possibly containing some zero values and some NaNs, replace any occurrences of zero or NaN with the character string 'error'. The output should be a cell array C of the same size as the input matrix. Each cell of the output cell array should either contain the corresponding entry of A if this is not zero or NaN, or the string 'error' otherwise.

Example:

If A = [1 0; NaN 1], then the output C should be the cell array C = {1, 'error'; 'error', 1}.

Solution Stats

52.09% Correct | 47.91% Incorrect
Last Solution submitted on Oct 05, 2025

Problem Comments

Solution Comments

Show comments
Join Cody Contest 2025 — Have Fun and Win Prizes!
...
We’re excited to invite you to Cody Contest 2025! 🎉 Pick a team,...

Problem Recent Solvers101

Suggested Problems

More from this Author4

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!