Filter löschen
Filter löschen

columns data to reference rows data

1 Ansicht (letzte 30 Tage)
Farman Shah
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

Akzeptierte Antwort

Guillaume
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)
  1 Kommentar
Farman Shah
Farman Shah am 12 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

TADA
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
Farman Shah
Farman Shah am 12 Dez. 2018
thanks to you too......
TADA
TADA am 12 Dez. 2018
cheers

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by