MATLAB How to Calculate Squares and Cubes in MATLAB?

AI Thread Summary
To find the square and cube of the numbers 1, 2, and 3 using MATLAB, start by creating a vector x with the values [1 2 3] and a vector y with the values [2 3]. Utilize the meshgrid function to generate two matrices from these vectors, where the first matrix corresponds to the elements of vector x and the second matrix corresponds to vector y. After creating the matrices, apply the element-wise power operator .^ to raise the elements of the first matrix to the powers specified in the second matrix. This will yield a final matrix with the squares and cubes of the original numbers, structured in two rows and three columns. The discussion highlights the importance of following the exercise instructions and experimenting with the .^ operator to achieve the desired results.
ineedhelpnow
Messages
649
Reaction score
0
We want to know the square and the cube of each of the following numbers: 1, 2 and 3. You can have MATLAB obtain these results as follows.
Create a vector [1 2 3] . Create a vector [2 3]. Use these two vectors as inputs to the meshgrid function (with the 3-element vector as the first argument), which will produce two matrices. Each of these matrices will have 2 rows and 3 columns. The first matrix will have rows that equal the first vector. The second matrix will have columns with the elements of the second vector. Use the operator .^ (element-by-element raising to a power) to generate a matrix with the desired answers. (This final matrix should have 2 rows and 3 columns.)

hi (Wave)
so here's what i did so far
Code:
x=[1 2 3];
y=[2 3];
[x,y]=meshgrid(x,y)

im lost on what to do next :o
 
Physics news on Phys.org
So you have created two matrices and have two original vectors.

Have you tried to do what the exercise told you to use? Make a couple of combinations and see how .^ work and you will soon see the answer, that is, do what your teacher want you to do :)
 

Similar threads

Back
Top