columns data to reference rows data
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Farman Shah
am 12 Dez. 2018
Kommentiert: TADA
am 12 Dez. 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
0 Kommentare
Akzeptierte Antwort
Guillaume
am 12 Dez. 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)
Weitere Antworten (1)
TADA
am 12 Dez. 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
2 Kommentare
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!