MATLAB Efficiently Evaluate z Values in Matlab using Formula - 15+ Data Points

  • Thread starter Thread starter Benny
  • Start date Start date
  • Tags Tags
    Formula Matlab
AI Thread Summary
To evaluate values of z based on given data points in Matlab, the element-wise operation .^ is essential for applying formulas to vectors. For example, using z = x.^2 + y.^6 allows for the computation of z for each corresponding element in vectors x and y. When dealing with larger datasets, such as 50 or more data points, manually entering vectors becomes impractical. Instead, users can import data from Excel into Matlab. This can be achieved through Dynamic Data Exchange (DDE) or by saving Excel columns as a text file and using the load command in Matlab. Once imported, the data can be manipulated as a matrix, allowing for efficient calculations of z using commands like z = test(:,1).^2 + test(:,2).^4. Users have successfully tested this approach by pasting data into Matlab, confirming its effectiveness for larger datasets.
Benny
Messages
577
Reaction score
0
If I have some data for example (x_1,y_2) = (1,1), (x_2,y_2) = (2,2) and (x_3,y_3) = (3,3) [they aren't coordinates, it's just a convenient way to show the information] and I want to evaluate the values of z (z_1, z_2 and z_3) where for example z = x^2 + y^6, can I enter the formula for z into Matlab to do this for me?

For just 3 bits of data I can do it by hand but if I have 15+ then it gets tiresome so I'd like to know if I can use Matlab to apply a formula to data (like the example above) to get out the required values. Any help would be good, thanks.
 
Physics news on Phys.org
I suppose that by x and y you mean two column vectors. The operation ^ is not defined for vectors, only for scalars and square matrixes, but in Matlab you can use the elementwise operation .^:
z = x.^2 + y.^6
 
So for example could I do the following to obtain values of z?

x = [1 2 3 4];
y = [1 2 3 4];
z = x.^2+y.^4


That sort of method is ok for a few values. But if I have 50 or so pieces of data then it's impractical to enter column vectors with 50 elements into Matlab. Is it possible to transfer/import, the data entered into the worksheets of Excel, to Matlab and use commands in Matlab to apply some function (for example z = x^2+y^2) on the data (x,y) to obtain values of z?
 
Benny said:
So for example could I do the following to obtain values of z?

x = [1 2 3 4];
y = [1 2 3 4];
z = x.^2+y.^4
Yes, this is a valid command.
That sort of method is ok for a few values. But if I have 50 or so pieces of data then it's impractical to enter column vectors with 50 elements into Matlab. Is it possible to transfer/import, the data entered into the worksheets of Excel, to Matlab and use commands in Matlab to apply some function (for example z = x^2+y^2) on the data (x,y) to obtain values of z?
For use of Matlab with Excel and other Windows applications, read the Matlab Help about Dynamic Data Exchange (DDE).
Alternatively, you can save the columns of your Excel worksheet as a text file and use the load Matlab function to import them as a matrix.
For instance, suppose you have a worksheet with 2 columns and you save it as test.txt. In Matlab you can use the command load test.txt in the appropriate directory. You have in your work directory a matrix named test.
Now you can use the command z = test(:,1).^2 + test(:,2).^4 to get your result.
 
Ok thanks. I ended up just trying testing some things by putting in some random numbers into a column of excel, pasting it into a 'half-complete' vector in Matlab, closed out the vector with the square bracket na dthen applied some element wise operations. It worked so that was good.
 

Similar threads

Back
Top