필터 지우기
필터 지우기

Find value in second column when first column is x

조회 수: 9 (최근 30일)
Giuseppe Bregliozzi
Giuseppe Bregliozzi 2012년 3월 22일
Hi,
i would like to find the value in a second column corresponding to a value of the first column: [9 0.1; 16 0.2;3 1;89 6;5 0.4;77 0.8]
If I'm searching the value 3 the answer is 1 and if I'm searching 5 the answer is 0.4. I would always search for these numerical values in the first column, but I do not know their row position. Is there an easy and simple wait to do it? Thanks a lot. I have just started using Matlab.

채택된 답변

Wayne King
Wayne King 2012년 3월 22일
indices = find(A(:,1) == 3);
A(indices,2)
Or
indices = find(A(:,1)>2 & A(:,1) <6);
A(indices,2)
Or
A(find(A(:,1)>2 & A(:,1)<6),2)
Another way is:
locs = ismember(A(:,1), [3 5]);
A(locs,2)

추가 답변 (1개)

Geoff
Geoff 2012년 3월 22일
The find function will give you the row numbers. Type:
help find
But if you don't care about the row indices, the preferable way is to use logical indexing:
>> x = [9 0.1; 16 0.2; 3 1; 89 6; 5 0.4; 77 0.8];
>> x(x(:,1)==5, 2)
ans =
0.4

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by