r/matlab•
Varboa

Efficient ways of setting up a matrix

Hey guys, thanks in advance, I'm quite new to matlab!

So I'm looking to create a nxm matrix which for every element is a function of two variables p and t. The catch is, I need p to remain constant in the rows, and t to progress, and t to be constant in the columns, and p to vary. I would like the vectors p and t to have easy to input start stop and increment values.

I've considered decomposing the function into elementry steps, and then applying matrix algebra to apply successive transforms on the elements of my matrix, until the over all transform is the function value at each element of the matrix, I've also just found out about cat() and I'm thinking about constructing a row vector matching what I need, and then running a loop, creating more vectors, with slightly different p values, and then cat them together?

Thanks guys! I cant help but feel like I'm missing something easy here

Posted Feb 3, 2019, 10:39 PM UTC

211
maedhros11•

Not sure if I'm correctly interpreting what you're looking for, but it sounds like you're describing something that you could construct from the outputs of the meshgrid function.

5
VarboaOP•

I was thinking the same thing, but I wasnt sure if I could then define a function f(t,p) and then use each t and p value at each element of the array, and replace it in the matrix with the function value, considering t and p, as its inputs.

My end result would be an array of f values which took in t and p values based on its position in the matrix, based on how I define t and p in the first place?

2
maedhros11•

Again, I'm not 100% sure that we're on the same page, but if I understand you correctly then this is basically exactly the purpose of meshgrid.

I'll post an example code snippet once I'm back on my computer (on mobile currently and heading to bed shortly, so not until tomorrow)

1
VarboaOP•

Okay, I am familiar with meshgrid, but I would like help with plugging in the values of t and p at that element, into the function f

1
maedhros11•

Sorry I haven't gotten to this earlier. What does your function f(t,p) look like?

1

So correct me if I'm misunderstanding this, but you want a matrix of elements akin to something like this:

M = [p*t, p*t^2, p*t^3, ... , p*t^m;
     p^2*t,
     p^3*t,                    ... ;
     ...,
     p^n*t,       ...         p^n*t^m];

Where the dimensions of the matrix are defined by the length of the p and t vectors, which are 1xn and 1xm, respectively. If this is the case, then by transposing the p vector to become nx1, then you can multiply the two vectors against one another and they should create the matrix you want.

p = [1xn];
t = [1xm];

M = p'*t = [nx1]*[1xm] = nxm
1
VarboaOP•

Your notation implies exponitiation? Is that just notation? Essentially if i imagine myself walking along the first row, i should only be progressing through the t vector, and my p value should be constant, and if i walk down the first column, i should vary in my p vector but not in my t vector. This is the domain of a function, and I would I like to apply a function f(t,p) to the entire matrix.

2

That was just notation to implicate that the functions of p or t change within each element of their respective vectors and consequently would change within the matrix.

1
FrickinLazerBeams+2•

Can you define it in a separable way, such that the final matrix has elements which are described by f(p) * g(t)? If so you can do an outer product to get the matrix.

Alternatively you can use meshgrid.

1
VarboaOP•

I think I'm looking for meshgrid, yup

1