How do I create a set of variables from specific coordinate values in a square matrix?

6 views (last 30 days)
I have a 30x30 matrix of numeric values. The horizontal and vertical coordinates are each the same 30 locations, with the values in the matrix being the distance between one location to another (x,y). I've calculated a 30x1 vector with a specific 'path' through the matrix locations. I am trying to create a set of 30 variables with the matrix values for 30 matrix coordinates, where each variable is the value of the coordinate in the matrix corresponding to (the first 'path' value, the next path value).
So given the distance matrix (dm) 30x30
and a 'path' vector 30x1 with a given order of the numbers 1 through 30
how to create 30 variables, each with the dm values of one path number to the next
example
6x6 matrix given path=[2;4;6;1;3;5] var01=matrix value at (2,4) var02=matrix value at (4,6) var03=matrix value at (6,1) var04=matrix value at (1,3) var05=matrix value at (3,5) var06=matrix value at (5,2) <------ last y value=first path value
This is applied in biology for ranking multiple sequence alignments. I am new to Matlab, and I appreciate any help with this problem, it is beyond my current skill level. This is for a project in my bioinformatics class, I'm going for a computational biology degree.
thx
  1 Comment
Adam Quintero
Adam Quintero on 5 Jun 2011
a more general question would be, how do I set up a loop that outputs a new, sequentially named variable every pass? I could incrementally read the path vector each loop, and create a variable of the corresponding matrix value. I am quite unclear how to code for the automated variable creation.
Also, am I going about this the right way? Are there functions that automatically name and define variables? I've looked through the documentation, and I can't find anything like that.
any suggestions will be appreciated

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 5 Jun 2011
% Some data to work with...
A = magic(6);
path = [2;4;6;1;3;5];
% The engine to get the path through A...
path = [path;path(1)];
path = path(cumsum([[1 2];ones(size(path,1)-2,2)])) % Path matrix
var = A(path(:,1)+(path(:,2)-1)*size(A,1))
% var(1) is A(2,4), var(2) is A(4,6), var(3) is A(6,1), etc...
  1 Comment
Adam Quintero
Adam Quintero on 5 Jun 2011
perfect. i see the first line of the engine adds the first path to the end, completing the path. the second line defines the coordinates for each distance value as (current#,next#) in the sequence.line three then adds those values to the var vector.
thank you

Sign in to comment.

More Answers (1)

Paulo Silva
Paulo Silva on 5 Jun 2011
  4 Comments
Matt Fig
Matt Fig on 5 Jun 2011
Let me ask you, why not just have one variable named var which has all of your values? Then var(1) is equal to your var01, var(30) is equal to your var30. Why not do this? A vector var is much easier to deal with than 30 different variables!
Adam Quintero
Adam Quintero on 5 Jun 2011
i see what you are saying,good point. what is the best approach to automating the vector building,
from the [2;4;6;1;3;5]
into
[2 4
4 6
6 1
1 3
3 5
5 2]
then pathmatrixvalues = [(2,4)(4,6) (6,1) (1,3) (3,5) (5,2)]
where the pathmatrixvalues vector contains the 6 matrix values of those coordinates.
I am trying to get the length of the path sequence n and the nxn distance score matrix as a general format for the input to this process, where I can use a path and matrix of varying sizes.
i am very interested in learning ways to automate patterned tasks. I can see the pattern here, just not the code to implement it. thanks for all responses, 'Answers' section has some smart cats :)

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!