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

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

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

% 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

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)

4 Comments

As the FAQ says, you really should figure out a different way to do what you want to do. It is a bad idea to fill your workspace with many variables because then your next question will be, "I have all of the variables and I need to do X with them but I can't figure out how?" Then after you figure that out, your complaint will be, "I have an error and I can't figure out where because it occurs in and EVAL statement." or "Why is my code sooo slow?"
that is the main question here, i want to be able to scale this up, so the brute force of typing out each of the 30 path coordinates into separate variables is to be avoided. there has to be a way to tell matlab make 30 variables named var01-var30, and assign the distance matrix values to each of them based on the path sequence. I don't know what that is though....
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!
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

Community Treasure Hunt

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

Start Hunting!