How to find a value in column 2 which corresponds to particular value in column 1?

조회 수: 9 (최근 30일)
Let say I have a 2 colomn matrix A=[1 3 7 9 4 5 3 2 4; 2 3 1 3 4 5 9 2 4], Based on the value particular vaue in colomn 1 how can I find corresponding value in B for example when I have a value of 9 in then how can I look for corresponding value in colomn 2?

채택된 답변

Image Analyst
Image Analyst 2022년 3월 26일
I think it's ambiguously worded but I tried to follow your directions as precisely as I could:
format short g
% Define A with 2 rows and 9 columns
A=[1 3 7 9 4 5 3 2 4; 2 3 1 3 4 5 9 2 4]
A = 2×9
1 3 7 9 4 5 3 2 4 2 3 1 3 4 5 9 2 4
% Define B
B = rand(size(A))
B = 2×9
0.15297 0.37938 0.683 0.88068 0.96106 0.65517 0.19194 0.51023 0.1633 0.55587 0.12864 0.91176 0.37441 0.20156 0.88471 0.36068 0.58696 0.97853
% Poster says "Based on the value particular vaue in colomn 1
% how can I find corresponding value in B?
% For example when I have a value of 9 in then how can I look for corresponding value
% in colomn 2?"
% Find a particular value in A's column 1.
% The location of that value was not specified by the poster.
% Let's say that you want the value in the first row of column 1 of A.
value = A(1,1) % = 1 for this example.
value =
1
% Now assume that value we want that value from the corresponding row (1)
% of matrix B but in column 2. In other words B(1, 2)
output = B(value, 2)
output =
0.37938
  댓글 수: 10
Image Analyst
Image Analyst 2022년 3월 27일
Node1 is either true or false, not an index like you'd get from find()
Node1 = find(Slope_Indices(:,1) == Node);
I think you can invest 2 hours in this
and learn how to do it yourself. It's just basic indexing. If you can't do this then you're going to have trouble with every other step beyond this. So learn the basics and you'll be self sufficient.

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

추가 답변 (1개)

KSSV
KSSV 2022년 3월 26일
A=[1 3 7 9 4 5 3 2 4; 2 3 1 3 4 5 9 2 4] ;
idx = A(1,:)==9
idx = 1×9 logical array
0 0 0 1 0 0 0 0 0
iwant = A(2,idx)
iwant = 3
  댓글 수: 1
Muhammad Qaisar Fahim
Muhammad Qaisar Fahim 2022년 3월 26일
Thanks @KSSV. Actually the above answer is talking about ndexing. Actually I dont want simple indexing. I want value in row two which correspond to the value in row one. Not corresponding to the index of row 1

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

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by