Getting 1 value from another.

1 次查看(过去 30 天)
James Connor
James Connor 2015-10-24
评论: Jan 2015-10-25
Lets say we have an array
20 4
21 5
22 3
23 2
24 1
25 2
26 7
if that table say =x how do I return a value in the first row from the second for example if I have x(:,2)==7 give ans=26 (first column corresponding to 7). Another example would be x(:,2)==2 give ans=25 Thanks

回答(1 个)

Jan
Jan 2015-10-24
x = [20 4; ...
21 5; ...
22 3; ...
23 2; ...
24 1; ...
25 2; ...
26 7]
search = 2;
index = (x(:, 2) == search);
result = x(index, 1)
  3 个评论
James Connor
James Connor 2015-10-24
Sorry I didnt explain my question well, I mean for like max(x(:,2)); which would be 7 thus output a value of 26.
Jan
Jan 2015-10-25
Perhaps you mean:
[value, index] = max(x(:, 2));
x(index, 1)

请先登录,再进行评论。

类别

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