How do i get element array from find function?
이전 댓글 표시
I know that the find function gives me an array with the location of elements. How do I convert those location indices into an array that displays the element instead?
답변 (1개)
Image Analyst
2017년 2월 18일
If you have a 1-D vector, it gives the indexes directly. If you have a 2-D matrix, it gives rows and columns,
[rows, columns] = find(yourArray = someValue);
I don't know what you mean by the element - is it the linear index, the row(s) and column(s), or the array element itself? If you're testing a range instead of equality, you can use the linear index instead of the rows and columns to get the values:
m = magic(5);
linearIndexes = find(m>=18)
result = m(linearIndexes)
댓글 수: 5
Anonymous Matrix
2017년 2월 18일
Stephen23
2017년 2월 18일
All of these very basic features of MATLAB are introduced and explained in the Introductory Tutorials, which are highly recommended for all beginners:
find returns indices. Indices are used to access elements of arrays. So use the indices:
>> e = [2,3,6;10,7,8];
>> x = find(e>4);
>> e(x)
ans =
10
7
6
8
In this case your code would be simpler and faster if you do not use find at all and just use logical indexing (beginners love using find when it is not required):
>> e(e>4)
ans =
10
7
6
8
Anonymous Matrix
2017년 2월 18일
편집: Anonymous Matrix
2017년 2월 18일
@Anonymous Matrix: I (and other volunteers here) try to show beginners that they can also search for information in the documentation: the documentation has information on how MATLAB works, and you want information on how to use MATLAB... it is a match made in heaven! The more you practice searching for information in the documentation the more you will learn, and the better you will be at using MATLAB.
I don't expect beginners to know how to use MATLAB. I do encourage beginners to read the documentation: MATLAB has very readable documentation. Use it.
Star Strider
2017년 2월 18일
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!