columns data to reference rows data

1 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2018-12-12
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 个评论
Farman Shah
Farman Shah 2018-12-12
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.

请先登录,再进行评论。

更多回答(1 个)

TADA
TADA 2018-12-12
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

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by