{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

safeIndex - %the variable yes returns a true only if 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 %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);
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 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 %array of elements error_flag = ~all(yes); %if yes is true, then the ~ of all of the yes will return a true only if %all of the yes was true; otherwise the error will be false if the any part %of the yes is false arr_indexes(~yes) = ; %this removes the indexes where the false values were positioned to leave %the array of indexes with only the true positions and their respective %values arr1 = arr_elements(arr_indexes); %the new array of indexes that we updated will be used to index those true %elements in the array of elements that were true; this will return the %answer in the new array % end...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online