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일
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

Brilliant, Thank you so much!
Voss
Voss 2023년 2월 22일
You're welcome!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing 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