querying matrices using vectors (lookup table for matrices?)

2 views (last 30 days)
Hello,
Suppose A1, A2, etc. are same-size matrices which are valid within different ranges, x_min and x_max. Given x, I'd like to get the associated matrix A.
For instance:
A1 if x_min1 < x < x_max1
A2 if x_min2 < x < x_max2
etc.
My problem is, x is actually a vector (n-by-1), and therefore every element in vector x might have a different matrix associated with it. I'm trying to think of the best way of storing matrices for corresponding x values. The matrices will be used in later operations in the same function. Would a local function be the best option? If so, how would you define this local function? Or is there a better alternative? I thought of using cell arrays but I'm not very experienced with them. Also, size of vector x changes with every function call.
Please advise. Thank you.
----
Matrices and the ranges are stored in a Matlab structure in different fields. e.g.
struct(1).A = A1
struct(1).xranges = [x_min1; x_max1]
struct(2).A = A2
struct(2).xranges = [x_min2; x_max2]
etc.
Currently I'm calling these matrices in a very lame way:
Because the size of the vector x changes with every function call, I have a for loop that finds the index of struct for x_min < x < x_max, uses the matrix corresponding to this index, and then goes to the next element of x in the for loop. Ideally, I'd like to query all matrices corresponding to all elements of vector x once, keep this data in whatever format it is and use it later on.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!