Wikipedia:Reference desk/Archives/Mathematics/2018 September 29

From Wikipedia, the free encyclopedia
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[edit]

MatLab/Octave solve a function with specific candidates[edit]

I 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)[reply]

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)[reply]
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) ] );
Dragons flight (talk) 07:30, 1 October 2018 (UTC)[reply]

J code[edit]

  0=1 3 5+/1 2 3 4-6
0 0 0 0
0 0 1 0
1 0 0 0

Bo Jacoby (talk) 13:30, 4 October 2018 (UTC).[reply]