1

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!

1
  • 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? Commented Feb 25, 2014 at 1:03

2 Answers 2

0

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!)

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I'm sorry; I can't seem to make this work. Could you please elaborate or provide a link that gives more insight into this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.