columns data to reference rows data

2 views (last 30 days)
Farman Shah
Farman Shah on 12 Dec 2018
Commented: TADA on 12 Dec 2018
i have acolumn data of A=[1,4,2,5,4] in one variabe. the second variabel contains the probability of each element of A. the seceond variable B is type of data given below .
s.no 1 2 3 4 5
1 .3 .5 .6 .3 .1
B= 2 .4 .2 .7 .9 .2
3 .3 .3 .4 .8 .9
4 .5 .1 .4 .5 .6
5 .2 .3 .6 .8 .3
Now as: in A the first elemetn is 1 so i want to search in B at seriol no (1,1) which is .3, the second element in A is 4 so i want to search it at location (4,4) which is .5 and so on for all the elements in A , and keep these values in another variable?
thanks

Accepted Answer

Guillaume
Guillaume on 12 Dec 2018
It really doesn't help if you invent your own syntax for your examples instead of using valid matlab syntax. It also doesn't help if you write .5 in you example then say that the value is 5 (10 times more).
Assuming I understood correctly:
A = [1, 4, 2, 5, 4] %note that this is a ROW vector not a column, but that doesn't matter anyway
B = [3 5 6 3 1
4 2 7 9 2
3 3 4 8 9
5 1 4 5 6
2 3 6 8 3]; %I'm assuming all these . in your example are not meant to be there. The values don't matter anyway
%option 1:
result = B(sub2ind(size(B), A, A))
%option 2:
diagB = diag(B).';
result = diagB(A)
  1 Comment
Farman Shah
Farman Shah on 12 Dec 2018
thank you very much sir. Actually its the values in points. sorry for bad formatting. thanks for your valueable responce. Option one is correctly working.

Sign in to comment.

More Answers (1)

TADA
TADA on 12 Dec 2018
you can use the values in A as indices for B if you translate to linear indices:
A = [1,4,2,5,4];
B = [3, 5, 6, 3, 1;
4, 2, 7, 9, 2;
3, 3, 4, 8, 9;
5, 1, 4, 5, 6;
2, 3, 6, 8, 3];
C = B(sub2ind(size(B), A,A))
C =
3 5 2 3 5

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!