Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a $n \times k$ Matrix of data that I want to feed into an equation and solve for an unknown variable. I suppose I can always just convert the Matrix into a long Array but I'd like to learn how to do this while keeping the data structure as is.

I want to be able to solve $n \times k$ FindRoots in one shot and mimicking some code from MMA.SE this is what I have managed.

f = x^(Mod[a, 3]) + b
a = {{2, 5, 8}, {1, 4, 7}};
b = {{-3, -5, -7}, {3, 5, 7}};
eqn = Table[(#[[j]] == 0), {j, 1, Dimensions[f][[2]]}] & /@ (f)
{{-3 + x^2 == 0, -5 + x^2 == 0, -7 + x^2 == 0}, {3 + x == 0, 5 + x == 0, 7 + x == 0}}
Table[FindRoot[#[[i]], {x, 1}] & /@ eqn, {i, 1, Dimensions[f][[2]]}]
{{{x -> 1.73205}, {x -> -3.}}, {{x -> 2.23607}, {x -> -5.}}, {{x -> 2.64575}, {x ->7.}}}

As you can see, this works but is a kind of a mess. I am hoping I can be helped in the direction of a cleaner, better code and learn how to use pure functions on n-dimensional lists. Thanks.

share|improve this question
1  
What ? No xkcd? –  belisarius Oct 3 '12 at 1:50
    
The f should be after a and b, no? –  J. M. Oct 3 '12 at 1:56
    
@belisarius lol.. –  Amatya Oct 3 '12 at 2:00
    
@J.M. It works either way. –  Amatya Oct 3 '12 at 2:01
2  
@Amatya I can tell you that I posted my answer solely in the hopes of going viral. :) –  Mark McClure Oct 3 '12 at 2:13
show 1 more comment

1 Answer 1

up vote 5 down vote accepted

Here's one approach:

eq[a_, b_] := x^Mod[a, 3] + b == 0;
a = {{2, 5, 8}, {1, 4, 7}};
b = {{-3, -5, -7}, {3, 5, 7}};
eqArray = MapThread[eq, {a, b}, 2];
Map[FindRoot[#, {x, 1}] &, eqArray, {2}]

(* Out: {
  {{x -> 1.73205}, {x -> 2.23607}, {x -> 2.64575}}, 
  {{x -> -3.}, {x -> -5.}, {x -> -7.}}
} *)

Note specifically, that the MapThread command gets functionally right to the heart of the combination you desire. Here's a 1D version.

Clear[a, b, f];
MapThread[h, {{a, b, c}, {d, e, f}}]

(* Out: {h[a, d], h[b, e], h[c, f]} *)
share|improve this answer
    
The == 0 in eq[] can of course be excised. :) You can then do something like With[{a = {{2, 5, 8}, {1, 4, 7}}, b = {{-3, -5, -7}, {3, 5, 7}}}, Map[FindRoot[#, {x, 1}] &, x^Mod[a, 3] + b, {2}]] –  J. M. Oct 3 '12 at 2:05
1  
@J.M. You are one critical dude!!! :) –  Mark McClure Oct 3 '12 at 2:07
    
I still upvoted your answer, tho. :) I posted a comment as opposed to an answer since this is essentially equivalent. –  J. M. Oct 3 '12 at 2:08
    
@J.M. I know. I definitely appreciate your always insightful comments. Although, you could shorten your name. –  Mark McClure Oct 3 '12 at 2:12
    
Re: the addendum, if h is in fact a Listable function, then h[{a, b, c}, {d, e, f}] is equivalent to the MapThread[] construction. –  J. M. Oct 3 '12 at 2:14
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.