How to identify a particular range of y values corresponding to a specif range of x

조회 수: 1 (최근 30일)
I have a matrix A=[1 2 3, 4 5 6, 7 8 9] and a corresponding matrix B=[11 12 13, 14 15 16, 17 18 19]. i want to find out a range of B values corresponding to matrix A. for example:
For A1=[1 4 7] corresponding B1=[11 14 17] and for A2=[2 6 8] corresponding B2=[ 12 15 18]
how can i do that

답변 (1개)

Star Strider
Star Strider 2015년 7월 21일
First, I believe you intend to replace the commas in ‘A’ and ‘B’ with semicolons. After that, you simply do matrix addressing:
A=[1 2 3; 4 5 6; 7 8 9];
B=[11 12 13; 14 15 16; 17 18 19];
A1 = A(:,1)
B1 = B(:,1)
A2 = A(:,2)
B2 = B(:,2)
A1 =
1
4
7
B1 =
11
14
17
A2 =
2
5
8
B2 =
12
15
18
  댓글 수: 2
navan
navan 2015년 7월 21일
Dear Strider,
Thanks a lot for your answer. But i exactly needed it to be done in a different way. 1) i want to identify B matrix corresponds to matrix A.(not necessary a full collumn) for example.
A1=[1 4] should fetch an answer B1=[11 14]
2) when it should work for a selected array of A values. how can i use it
example : A1= [1 2 3, 4 5 6] should fetch an answer B1=[11 12 13, 14 15 16]
Star Strider
Star Strider 2015년 7월 21일
I am not sure what you want to do.
Experiment with the matrix indexing I outlined earlier. See the documentation on Matrices and Arrays for a full description of array indexing.
If you want to get the corresponding indices of specific unique values in ‘A’ and get the elements with corresponding indices in ‘B’, you can do something like this:
A1idx = find((A == 1) | (A == 4));
B1 = B(ind2sub(size(A), A1idx))
B1 =
11
14
I will leave it to you to read the documentation on ind2sub and experiment with the code.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by