Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:42:03+00:00 2026-05-27T11:42:03+00:00

I fill unused elements in a matrix with NaNs, and I would like to

  • 0

I fill unused elements in a matrix with NaNs, and I would like to assign a contrasting color to those elements with the NaN value when displaying the data using imagesc.

Below is a link to a possible solution, but I don’t quite understand it.

http://www.mathworks.com/matlabcentral/newsreader/view_thread/19985

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:42:04+00:00Added an answer on May 27, 2026 at 11:42 am

    NaN values get the first color from the axes colormap, which by default corresponds to the minimum value (other than NaN). You can change the color for minimum value setting axes color limits with CAXIS function. To assign a contrast color to NaN values you can add a special color for NaN values as a first color (1×3 vector).

    I take your example and made a function (with some comments):

    function [h hcb] = imagescwithnan(a,cm,nanclr)
    % IMAGESC with NaNs assigning a specific color to NaNs
    
    %# find minimum and maximum
    amin=min(a(:));
    amax=max(a(:));
    %# size of colormap
    n = size(cm,1);
    %# color step
    dmap=(amax-amin)/n;
    
    %# standard imagesc
    him = imagesc(a);
    %# add nan color to colormap
    colormap([nanclr; cm]);
    %# changing color limits
    caxis([amin-dmap amax]);
    %# place a colorbar
    hcb = colorbar;
    %# change Y limit for colorbar to avoid showing NaN color
    ylim(hcb,[amin amax])
    
    if nargout > 0
        h = him;
    end
    

    Here caxis statement assigns the first color of the color map not to the minimum value amin, but to the amin-dmap. So the first color get assigned specifically to NaNs.


    Try this function with:

    a=peaks;
    a(a < 0.5) = nan;
    imagescwithnan(a,hot,[0 1 1]) %# [0 1 1] is cyan
    

    test image - NaN color is hidden

    If you comment the ylim statement in the function (can be control with additional parameter) this NaN color will be on the colormap.

    test image - NaN color is shown on the colorbar

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to fill a table with the results of a query on
I would like to fill a select box with year starting from 1950 to
I fill the data set twice, the table name never set correctly. I see
I am trying to fill up an NSmutableAray with data from sqlite database. ItemShow
To fill one variable with a value from a query I can write following:
Fill the form with data Proceed to fields' validation if the validation pass, go
I need to fill a UITableView from an NSSet data source. Obviously I want
i fill a tdictionary , read from a file, to iterate over the key-value-pairs.
I fill the treeview with an object that contains id and its value, so
I fill a data table with 11 columns and 1 row. columns/ User Pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.