safeIndex - %the true positions in order to index to the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function [error_flag arr1] = safeIndex(arr_elements,arr_indexes) %function to determine if the inputted index array is valid to be used on %the inputted array of elements; if it is valid, your function should %return a FALSE error flag and a vector of the elements specified by the %index array; if your index array is not valid, your function should return %a TRUE error flag and a vector of the elements that occur at valid indices %usage: [error_flag arr1] = safeIndex(arr_elements,arr_indexes if strcmp(class(arr_indexes),'logical') arr_indexes = find(arr_indexes); end %the above if statement determines if the class of the array of indexes is %a logical array; if it is, then the array of indexes will be set equal to
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: %the true positions in order to index to the new array later and only %return those positions in the original array of elements that were true in %those respective positions yes = arr_indexes>0 & mod(arr_indexes,1)==0 & arr_indexes<=length(arr_elements); %the variable yes returns a true only if the respective elements are %greater than zero and the elements are positive integers and if the length %of the array of indexes is less than or equal to the length of the %elemen error_flag = ~all(yes); arr_indexes(~yes) = ; arr1 = arr_elements(arr_indexes); a end...
View Full Document

Ask a homework question - tutors are online