필터 지우기
필터 지우기

How to find the another values in array

조회 수: 1 (최근 30일)
singh
singh 2015년 4월 18일
편집: Stephen23 2015년 5월 4일
Suppose I have a array A=
1 73.21 32.2
6 11.1 3.32
9 30.4 19.64
17 12.6 77.3
20 30.7 6.2
23 21 .9 42.5
Now I have another array which contain some values B =
1 9 20 23
Now I input a single no through input command called N=20
I wish to find that N(20) in column first and return corresponding values column second and column third in array A
Like it return value 30.7 and 6.2
  댓글 수: 1
pfb
pfb 2015년 4월 18일
Thanks for choosing my answer.
Anyway the other two other answers are perhaps better, in that they do not use "find".

댓글을 달려면 로그인하십시오.

채택된 답변

pfb
pfb 2015년 4월 18일
You could use the command "find".
% this finds the row (if any) at which N appears in the first column of A
r = find(A(:,1)==N);
% this returns the corresponding values in the 2nd and 3rd columns of A
v = A(r,[2 3]);
% note that this is a row vector if N appears only once in the first column of A
% It is a matrix if N appears more than once
% It is an empty vector if N never appears.
  댓글 수: 1
Stephen23
Stephen23 2015년 5월 4일
편집: Stephen23 2015년 5월 4일
find is slower and not at all required. Use Image Analyst's solution.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 4월 18일
Just use logical indexing. You don't even need find().
A=[...
1, 73.21, 32.2;
6, 11.1, 3.32;
9, 30.4, 19.64;
17, 12.6, 77.3;
20, 30.7, 6.2;
23, 21.9, 42.5]
% B is unused - not sure why it was even mentioned
B =[1 9 20 23];
N = 20; % Gotten from using the input() function.
row = A(:,1)==N % N must be an integer.
output = A(row, 2:3)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by