1
$\begingroup$

It looks like a silly question, but I could not find any answers online. Your help is greatly appreciated!

Say I have two lists now.

L1={{k},{2k+1},{3k+3},{4k},{5k-4},{6k}}; L2={">0","<0",">0",">0",">0","<0"}; (*it's in string form, mathematica wont let me run >0 without quotation marks*) 

and I want to form a matrix like this shown as result (no string form contained)

result={{k>0},{2k+1<0},{3k+3>0},{4k>0},{5k-4>0},{6k<0}} 

so that I can use Reduce to find the range of k for all elements in the result list. Thanks in advance!!

$\endgroup$

3 Answers 3

1
$\begingroup$

Use a different format from the start to avoid this complication.

L1 = {k, 2 k + 1, 3 k + 3, 4 k, 5 k - 4, 6 k}; L2 = {1, 2, 1, 1, 1, 2}; MapThread[{# > 0, # < 0}[[#2]] &, {L1, L2}] 
{k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0} 
$\endgroup$
1
  • $\begingroup$ Thanks Mr. Wizard! I will try this as well! $\endgroup$ Commented Feb 1, 2015 at 3:53
3
$\begingroup$

Also:

L2a = L2 /. {">0" -> (Greater[#, 0] &), "<0" -> (Less[#, 0] &)}; MapThread[Apply, {L2a, L1}] (* {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0} *) 

Or

Apply @@@ Thread[{L2a, L1}] (* {k > 0, 1 + 2 k < 0, 3 + 3 k > 0, 4 k > 0, -4 + 5 k > 0, 6 k < 0} *) List /@ Apply @@@ Thread[{L2a, L1}] (* {{k > 0}, {1 + 2 k < 0}, {3 + 3 k > 0}, {4 k > 0}, {-4 + 5 k > 0}, {6 k < 0}} *) 
$\endgroup$
2
  • $\begingroup$ Congratulations on entering the 60K club! :-) $\endgroup$ Commented Jan 30, 2015 at 8:57
  • $\begingroup$ Thanks for your answer, kguler! $\endgroup$ Commented Feb 1, 2015 at 3:53
2
$\begingroup$

How about

ClearAll[k] L1 = {{k}, {2 k + 1}, {3 k + 3}, {4 k}, {5 k - 4}, {6 k}}; L2 = {">0", "<0", ">0", ">0", ">0", "<0"}; result = MapThread[{ToExpression@StringJoin[ToString[First@#1], #2]} &, {L1, L2}] 

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ It works like a charm! Thanks Nasser! $\endgroup$ Commented Jan 30, 2015 at 2:28
  • $\begingroup$ Sorry to bother you. It seems that the code does not work with symbol with subscript. For example, replace k with Subscript[k, 1]. I found this problem because I have all symbols with subscripts. It there a way to work around this? Maybe change the forms $\endgroup$ Commented Jan 30, 2015 at 2:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.