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
0 件のコメント
採用された回答
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 件)
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 Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!