Wikipedia:Reference desk/Archives/Mathematics/2018 September 29
Mathematics desk | ||
---|---|---|
< September 28 | << Aug | September | Oct >> | Current desk > |
Welcome to the Wikipedia Mathematics Reference Desk Archives |
---|
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
September 29
editMatLab/Octave solve a function with specific candidates
editI can do this with a for loop, but I assume that there is a built in function that does this faster...
Assume I have two candidates, such as x=[1,3,5] and y=[1,2,3,4]. I have a formula: f=x+y-6. I want to solve this to get all pairs x,y where f is 0. What is the function that does that? What is the syntax? 71.12.10.227 (talk) 13:02, 29 September 2018 (UTC)
- Not really familiar with MatLab (though maybe I should learn), but I'll try to get the ball rolling a bit. I think what you're looking for is something like Python's list comprehension, but my understanding is MatLab emphasizes array level operations so list comprehension isn't really needed. Of course there my be a combination of array level operations that gives you what you need, I don't know enough about MatLab to be sure. You might try the computing desk (just link here rather than cross post), or Stack Overflow. --RDBury (talk) 07:17, 1 October 2018 (UTC)
- I assume by x=[1,3,5] and y=[1,2,3,4] you mean you want to choose one element from the x list and one from the y list and see which pairs (x,y) satisfy f. If that is the case then the following code snippet would work:
- [X, Y] = meshgrid( x, y );
- fx = ( f(X, Y) == 0 );
- disp( [ X(fx), Y(fx) ] );
0=1 3 5+/1 2 3 4-6 0 0 0 0 0 0 1 0 1 0 0 0