Student Support Forum
-----
Student Support Forum: 'Problem with a function with two parameters' topicStudent Support Forum > General > Archives > "Problem with a function with two parameters"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic | Post New Topic
Author Comment/Response
Randy Silvers
05/29/08 8:08pm

Joel, you are close. You're right in that there is a problem with using brackets. Only use brackets to enclose arguments to a function.

Here, you want a function applying to two elements, each element being drawn from a separate list. Thus, both lists act as one argument to the Map function, and so need to be enclosed in brackets.

As you have it, Map at level 2 only will yield two lists, one list of the function evaluated at each element of matrixa, the other list of the function evaluated at each element of matrixb. Hardly what you want.

One way to solve your problem is to use MapThread.

MapThread[If[#1!=0,#1,#2]&,{matrixa,matrixb},2]

works as you desire.

It's a lot of practice, but it's worthwhile to understand the difference between Map, Apply, Thread, and MapThread.

Another way, using Map, would be to use Transpose to create a matrix of paired elements. If you had matrixab={{0,a},{1,b},{0,c},{2,d}} then

Map[If[#[[1]]!=0,#[[1]],#[[2]]]&,matrixab]

Hope that helps, at least to clarify the difference between Map and MapThread.

URL: sirandol@deakin.edu.au,

Subject (listing for 'Problem with a function with two parameters')
Author Date Posted
Problem with a function with two parameters Joel Santos 05/28/08 03:05am
Re: Problem with a function with two parameters Xavier 05/29/08 1:26pm
Re: Problem with a function with two parameters Randy Silvers 05/29/08 8:08pm
Re: Problem with a function with two parameters yehuda ben-s... 05/30/08 06:14am
Re: Problem with a function with two parameters Joel Santos 06/03/08 09:52am
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic | Post New Topic





 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: