Last Updated : 01 Nov, 2022
Comments
Improve
The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains the linear indices.
Using liner index a multidimensional array can be accessed using a single subscript. MATLAB treats the array as a single column vector with each column appended to the bottom of the previous column.
Example:
For example consider the following 3×3 array A =
1 4 7
2 5 8
3 6 9
See AlsoLocate functions and files - MATLAB whichThe find() Function in MATLABIndex eines Wertes in einem Array in Matlab findenErmitteln von Indizes und Werten von Elementen ungleich nullIn this array all elements represents their linear index i.e. we can reference A(1,2) with A(4).
Syntax:
Below are various ways to use the function:
- k = find(X): It returns the indices of all non zero elements.
- k = find(X, n): It returns the first n indices of non zero elements in X
- k = find(X, n, direction): direction can be ‘first’ or ‘last’. If direction is first, this function will return first n indices corresponding to non zero elements and if direction is last then this function will return last n indices corresponding to non zero elements
- [row, col] = find(): It is used to get row and column subscript for all non zero elements.
- [row, col, v] = find(): row and column will hold subscript for all non zero elements and v is a vector which will hold all the non zero elements.
Note: k will be of same orientation as X if X is a vector and if X is a multidimensional array then k will be a column vector which will hold linear indices.
Example 1: Below code will return the indices of non-zero elements in a 1-D array.
MATLAB
% Defining array
A = [1 2 3 0]
% Getting indices of non zero elements
find(A)
Output:
Example 2: Below code will return the first 2 indices of elements where the element will be greater than 3.
MATLAB
% Defining array
A = [1 2 0; 3 1 4; 5 6 7]
% Getting first 2 indices
find(A>3, 2)
Output:
Example 3: Below code will return the last 2 row and column indices of elements that are greater than 3.
MATLAB
% Defining array
A = [1 2 0; 3 1 4; 5 6 7]
% Getting row and column
[row, col] = find(A>3, 2,
'last'
)
Output:
So, A(2, 3) and A(3, 3) are the last elements that are greater than 3. We got (2, 3) and (3, 3) as output not (3,2) and (3, 3) because MATLAB treats the array as a single column vector with each column appended to the bottom of the previous column.
Example 4: Below code will return indices of all the zero elements. This code uses the negation operator (~) in conjunction with the find function.
MATLAB
% Defining array
A = [1 2 3 0]
% Getting indices of zero elements
find(~A)
Output:
${secondsToHms(duration)}
${getTagsString(tags)}
'; } else{ let view_all_url = `${GFG_SITE_URL}videos/`; videos_content+=`
View All
`; } // videos_content+= '
'; } } return videos_content; } //function to show main video content with related videos content async function showMainVideoContent(main_video, course_link){ //Load main video $(".video-main").html(`
`); require(["ima"], function() { var player = videojs('article-video', { controls: true, // autoplay: true, // muted: true, controlBar: { pictureInPictureToggle: false }, playbackRates: [0.5, 0.75, 1, 1.25, 1.5, 2], poster: main_video['meta']['largeThumbnail'], sources: [{src: main_video['source'], type: 'application/x-mpegURL'}], tracks: [{src: main_video['subtitle'], kind:'captions', srclang: 'en', label: 'English', default: true}] },function() { player.qualityLevels(); try { player.hlsQualitySelector(); } catch (error) { console.log("HLS not working - ") } document.getElementById('article-video-tab-content').style.display = 'block'; } ); const video = document.querySelector("video"); const events =[ { 'name':'play', 'callback':()=>{videoPlayCallback(main_video['slug'])} }, ]; events.forEach(event=>{ video.addEventListener(event.name,event.callback); }); // error handling for no compatible source player.on('error', function() { var error = player.error(); console.log("Video Error: ", error); if (error && error.code === 4) { console.log("No compatible source was found for this media."); hideVideoPlayer(); } }); }, function(err) { var player = videojs('article-video'); player.createModal('Something went wrong. Please refresh the page to load the video.'); hideVideoPlayer(); // hiding video in case of timeout (requirejs) console.log(err); }); // function to hide the video player function hideVideoPlayer() { var videoPlayer = document.getElementById('article-video'); if (videoPlayer) { videoPlayer.parentNode.removeChild(videoPlayer); } } /*let video_date = main_video['time']; video_date = video_date.split("/"); video_date = formatDate(video_date[2], video_date[1], video_date[0]); let share_section_content = `
${video_date}
`;*/ let hasLikeBtn = false; // console.log(share_section_content); var data = {}; if(false){ try { if((loginData && loginData.isLoggedIn == true)){ const resp = await fetch(`${API_SCRIPT_URL}logged-in-video-details/${main_video['slug']}/`,{ credentials: 'include' }) if(resp.status == 200 || resp.status == 201){ data = await resp.json(); share_section_content+= `
`; hasLikeBtn = true; } else { share_section_content+= `
`; } } else { share_section_content+= `
`; } //Load share section // $(".video-share-section").html(share_section_content); // let exitCond = 0; // const delay = (delayInms) => { // return new Promise(resolve => setTimeout(resolve, delayInms)); // } // while(!loginData){ // let delayres = await delay(1000); // exitCond+=1; // console.log(exitCond); // if(exitCond>5){ // break; // } // } // console.log(loginData); /*if(hasLikeBtn && loginData && loginData.isLoggedIn == true){ setLiked(data.liked) setSaved(data.watchlist) }*/ } catch (error) { console.log(error); } } //Load video content like title, description if(false){ $(".video-content-section").html(`
${main_video['title']}
${hideMainVideoDescription(main_video['description'], main_video['id'])}
${getTagsString(main_video['category'])} ${(course_link.length)? `
View Course
`:''} `); let related_vidoes = main_video['recommendations']; if(!!videos && videos.length>0){ //Load related videos $(".related-videos-content").html(getvideosContent()); } } //show video content // element = document.getElementById('article-video-tab-content'); // element.style.display = 'block'; $('.spinner-loading-overlay:eq(0)').remove(); $('.spinner-loading-overlay:eq(0)').remove(); } await showMainVideoContent(video_data, course_link); // fitRelatedVideosDescription(); } catch (error) { console.log(error); } } getVideoData(); /* $(window).resize(function(){ onWidthChangeEventsListener(); }); $('#video_nav_tab').click('on', function(){ fitRelatedVideosDescription(); });*/ });