NaN is used several ways in MATLAB. Working with NaN requires some attention to detail. Missing data Result of some computations...

Similar documents
LECTURE 1. What Is Matlab? Matlab Windows. Help

ECE-205 Lab 1. Introduction to Simulink and Matlab

Computational lab on complex numbers

Examples, examples: Outline

5. Control Statements

Assignment in Optimization 2016

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Data needs to be prepped for loading into matlab.

Repetition and Loop Statements Chapter 5

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

printf( Please enter another number: ); scanf( %d, &num2);

BioBIKE Language Syntax Working with large numbers of items: Mapping and Loops

While you are writing your code, make sure to include comments so that you will remember why you are doing what you are doing!

Our Strategy for Learning Fortran 90

New Concepts. Lab 7 Using Arrays, an Introduction

MATLAB: The greatest thing ever. Why is MATLAB so great? Nobody s perfect, not even MATLAB. Prof. Dionne Aleman. Excellent matrix/vector handling

Getting started with simulating data in R: some helpful functions and how to use them Ariel Muldoon August 28, 2018

Short Version of Matlab Manual

Lecture Transcript While and Do While Statements in C++

Practice Reading for Loops

Choosing the right graph in Excel

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Lecture 1: MATLAB - advanced use cases

CLAD Sample Exam 04. B. When you create an Array constant on the Block Diagram, it is not visible on the Front Panel.

Most nonzero floating-point numbers are normalized. This means they can be expressed as. x = ±(1 + f) 2 e. 0 f < 1

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

Modular and Verified Automatic Program Repairs

Chapter 9: Dealing with Errors

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

BIOE 198MI Biomedical Data Analysis. Spring Semester Dynamic programming: finding the shortest path

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

University of Massachusetts Lowell ECE Department Laboratory 1: Matrices, Vectors, and Matlab Editor and Real time debugger

Getting started with C++ (Part 2)

Certified LabVIEW Associate Developer Exam. Test Booklet

6 Further... Programming

Introduction to Scientific Computing with Matlab

How To Test Your Code A CS 1371 Homework Guide

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

Last class. A vertex (w x, w y, w z, w) - clipping is in the - windowing and viewport normalized view volume if: - scan conversion/ rasterization

Math Modeling in Java: An S-I Compartment Model

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

APPM 2460 PLOTTING IN MATLAB

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Repetition, Looping CS101

MATLAB Project: Getting Started with MATLAB

MATLAB Project: Getting Started with MATLAB

Lab 9 Loops, Debugging

Signed umbers. Sign/Magnitude otation

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Selection the If Statement Try Catch and Validation

Getting started with Hot Potatoes by Deborah Healey

Start Matlab and perform the following steps for each of the five sections: Start the Matlab editor by creating a new.m file.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

CS2141 Software Development using C/C++ C++ Basics

Floating-point representation

APPENDIX B. Fortran Hints

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Question Points Score Total 100

Designing Applications that See Lecture 4: Matlab Tutorial

We first learn one useful option of gcc. Copy the following C source file to your

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Stat 579: Objects in R Vectors

2 Computation with Floating-Point Numbers

Dinner for Two, Reprise

USER's GUIDE for PLOT_FINDIF_1

Warmup : Name that tune!

MATLAB An introductory tutorial

Module 1: Introduction RStudio

Assignment 2 in Simulation of Telesystems Laboratory exercise: Introduction to Simulink and Communications Blockset

Numerical Precision. Or, why my numbers aren t numbering right. 1 of 15

Intermediate MATLAB for Beginners

Matlab Introduction. Scalar Variables and Arithmetic Operators

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

CMSC 330: Organization of Programming Languages. Operational Semantics

Dr Richard Greenaway

Computing With R Handout 1

BioBIKE Language Syntax Working with large numbers of items: Mapping and Loops

Introduction to MATLAB

Chapter 1 Getting Started

SMS 3515: Scientific Computing Lecture 1: Introduction to Matlab 2014

function [s p] = sumprod (f, g)

Sheet 4. Example 1. a) Expand f(x) = x 2 in a Fourier series on [ π, π]. Use the even-odd idea to reduce your work.

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Meeting 1 Introduction to Functions. Part 1 Graphing Points on a Plane (REVIEW) Part 2 What is a function?

The Interpolating Polynomial

2 Computation with Floating-Point Numbers

PROBLEM SOLVING 11. July 24, 2012

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MARKING GUIDELINE NATIONAL CERTIFICATE (VOCATIONAL) NOVEMBER EXAMINATION. PRINCIPLES OF COMPUTER PROGRAMMING (First Paper) NQF LEVEL 3

A. Front Panel Design Lesson 4 Implementing a VI

Rollover. from xkcd.com

Exceptions in Python. AMath 483/583 Lecture 27 May 27, Exceptions in Python. Exceptions in Python

Programming for Experimental Research

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

Lab 1 - Worksheet Spring 2013

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Transcription:

MATLAB @ Work Working with Nan Richard Johnson NaN is used several ways in MATLAB. Working with NaN requires some attention to detail. Missing data... 1 Result of some computations... 3 Statistics and NaN... 4 Pre-allocation... 5 Issues... 6 Patterns for dealing with NaN... 7 Exercise... 7 Missing data NaN is the standard placeholder for missing data, and it is sometimes used as a replacement for "bad" data. A nice feature of nan is that MATLAB does not plot it, so that it can appear as a gap in a curve. d = [1 2 3 4 5 6]; h = [1 4 9 nan 25 36]; plot(d,h) richj@datatool.com www.datatool.com

Test for and locate nan values using isnan isnan(h) 1 Testing for equality to nan does not work. h==nan You might consider using the isfinite function, which will catch both NaN and Inf. isfinite(h) 1 1 1 1 1 It can be helpful to use a consolidator function. any(isnan(h)) 1 You can replace NaN values with code like index = find(isnan(h)); cleanh = h; cleanh(index) = mean([h(index-1) h(index+1)]) cleanh = 1 4 9 17 25 36 Of course code that deals with end effects and multiple NaN values will be more complicated. NaN is limited to double and single representations. uint16([ 1 nan]) 1 2

For integer types, you will need to develop your own value to represent missing data. Zero is usually not a good choice. Result of some computations Nan is the defined result for an indeterminate operation. When this occurs, MATLAB does not issue an error or warning. NaN can result from operations on Inf, but it most commonly occurs as a result of an operation on an array that contains a NaN. x = [1 2 nan 4]; y = sum(x) y = NaN You can have MATLAB stop in the Debugger in these cases. Select the option on the Stop if Errors window. This feature is accessed through the More Error and Handling Options on the Breakpoints pull down menu. 3

Statistics and NaN Some of the statistical functions ignore NaN. y = max([ 1 nan]) y = 1 y = min([ 1 nan]) y = Many statistical and aggregator functions return NaN when applied to values that contain NaN. y = sum([ 1 nan]) y = NaN y = median([ 1 nan]) 4

y = NaN Several of these functions have alternatives in the Statistical Toolbox that ignore NaN values. They have names like nansum, nanmedian, etc. You can also write similar versions. Pre-allocation Using nan to pre-allocate an array can be better than the traditional use of zeros because it can be easier to spot incomplete filling or incorrect elements. This can happen with a while or for loop that does not have enough iterations to fill the pre-allocated array. It can also happen if the iterator is misused. result = nan(1,8); for index = 1:2:16 result(index) = index; end result index result = Columns 1 through 13 1 NaN 3 NaN 5 NaN 7 NaN 9 11 13 Columns 14 through 15 15 5

index = or 15 result = nan(1,8); for index = 1:8 result(index+1) = index+1; end result index result = NaN 2 3 4 5 6 7 8 9 index = 8 Issues nan does not equal nan nan==nan So arrays that might appear to be the same or equivalent are not formally equal isequal([1 2 nan], [1 2 nan]) You can get around this behavior by using isequaln isequaln([1 2 nan], [1 2 nan]) 1 The older function isequalwithequalnans is available but not recommended. isequalwithequalnans([1 2 nan], [1 2 nan]) 1 nan is neither true 6

nan==true nor false. nan==false So you can't convert an array to logical if it contains a NaN. logical([1 nan]) Error using logical NaN's cannot be converted to logicals. Patterns for dealing with NaN The way that you process data that includes NaN values depends on what software you have available, and of course what you want to do. If you already have a function that does what you want then y = eff(x); An example is mean, if you want to have nan impact the result. Or perhaps y = naneff(x); if you want to ignore the NaN values. The function nanmean is an example. Of course you can also write a version like this yourself. Another way to ignore NaN values is something like y = x; y(~isnan(x)) = eff(x(~isnan(x))); or y = x; y(isnan(x)) = naneff(x(isnan(x))); Exercises Replace nan with an neighbor value. Use the values 7

t = linspace(, 2*pi, 5); z = sin(t); z(3) = nan; Plot the values. Replace the NaN value with the previous value (hold). Add a curve using this point to the plot. Highlight the replacement point. Write the code to be general, not just for the third point. Replace nan with an average value. Plot the values. Replace the NaN value with the average of the previous and following values. Add a curve using this point to the plot. Highlight the replacement point. Write the code to be general, not just for the third point. Replace nan with an interpolated value. Plot the values. Replace the NaN value with an interpolated value based on other values. Add a curve using this point to the plot. Highlight the replacement point. Write the code to be general, not just for the third point. Write a function nanmeanof. Write a function that works like mean but ignores nan values. Design it to work with arrays of one or two dimensions. 8