MATLAB Answers

Finding out values for elements of an array from corresponding array elements?

3 views (last 30 days)
Tha saliem
Tha saliem on 10 Jan 2018
Commented: Tha saliem on 10 Jan 2018
i have calculated weights given below between elements of a matrix using some mathematics
weights= {[0.79,0.31,0.24,0.71,0.1],[0.79,0.12,0.71,0.62,0.24]}
represents weight between 1 and 2,3,4,5,6 (0.79 for 1,2 ; 0.31 for 1,3 and so on) AND
represents weights between 2 and 1,3,4,5,6 (0.79 between 2,1 ; 0.12 between 2 and 3 and so on)
We can say that values of diagonal elements like 1,1 ; 2,2 are not there in weights.
Now I have another array X And I need weights of these elements from corresponding weights cell. LIKE for 3, weight of 1,3 will be taken as an output which is 0.31 in this case
Problem is that if i try to get this using loop: weight of 3 is at index position 2 in weights. For first row we can use loop using index-1. BUT when it comes to 2nd row this will not work weight for index 1 is at index 1 in weights{1,2}. But for 5 is at index 4 in weights{1,2}. confused about how to do this.
Also there are not just 2 rows. They are large in number.
Kindly help to solve this


Sign in to comment.

Accepted Answer

Thorsten on 10 Jan 2018
You can represent your weights as a matrix using NaN for the diagonal elements that do not exist:
Weights = [NaN, 0.79,0.31,0.24,0.71,0.1;
0.79, NaN, 0.12,0.71,0.62,0.24];
Then you can simply get the proper weight for row i, column j as
Weights(i, j)

More Answers (1)

dpb on 10 Jan 2018
I think simplest solution would be to just augment the weights array by a placeholder for the missing diagonal positions; then there's a straight 1:1 lookup. Use NaN as a marker if need a unique way to select the non-diagonal members only.


Community Treasure Hunt

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

Start Hunting!

Translated by