1
votes

I'm somewhat new to MATLAB and I'm trying to make a large matrix structured very much like the following example:

C=[1,2,3] 
n=[-3,0,3]
X=[f(1,-3),f(1,0),f(1,3);f(2,-3),f(2,0),f(2,3);f(3,-3),f(3,0),f(3,3)]

where f is some function of the values in C and n. I need this sort of matrix, X, so that I can make a surface graph of surf(C,n,X). The issue is that, in my real problem, I need a 51x51 matrix, and I don't know how to make such a matrix without typing out everything (which would be a horrific task).

Could someone please help me understand how to make such a plot? Thank you so much!

2
Does f operate element-wise on array inputs as well as just a pair of scalars (e.g. f(1,1)) as you have shown? - chappjc

2 Answers

0
votes

Have a look at ndgrid:

>> [ii,jj] = ndgrid(C,n)
ii =
     1     1     1
     2     2     2
     3     3     3
jj =
    -3     0     3
    -3     0     3
    -3     0     3

Then you can get X very easily if f operates element-wise on non-scalar inputs:

X = f(ii,jj);

If f can only take a pair of scalars, try arrayfun:

X = arrayfun(@(x,y)f(x,y),ii,jj);

(Or modify f!)

0
votes
X = f(C'*ones(1, 51), ones(51, 1)*n))