Extracting Corresponding values from an array

if I have 2 dimensional matrix , How can I create a function which takes , for example, a value from the first colmn in the matrix and ouputs the corresponding value in the the second colmn (and same row)?
Thank you so much!

 採用された回答

Voss
Voss 2023 年 2 月 22 日

0 投票

Example usage, function defined below.
matrix = [1 2; 3 4; 5 6; 1 8]
matrix = 4×2
1 2 3 4 5 6 1 8
get_2nd_column_val(matrix,3)
ans = 4
get_2nd_column_val(matrix,1)
ans = 2×1
2 8
% function definition
function out = get_2nd_column_val(data,in)
out = data(data(:,1) == in, 2);
end

2 件のコメント

Abdulrahman
Abdulrahman 2023 年 2 月 22 日
Brilliant, Thank you so much!
Voss
Voss 2023 年 2 月 22 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 2 月 22 日

コメント済み:

2023 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by