Message Boards Message Boards

0
|
5423 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Operating in Matrix Elements

Posted 10 years ago

Hi I was looking how to operate in just one subelement of a matrix or kind of list, this is what I want:

I have the next Matrix:

a1 = {{1, 43}, {123, 54}, {124, 78}}

I need to operate the first subelement of each element for example if I want to multiply by two, the expected out will be

={{2,43},{246,54},{248,76}}

And if its possible to aply a function to the first subelement of each element. For example imagine I have defined a function as follows:

g[h_, j_] :=(100 h)/j

I want the next

 {{g[1, 1],43},{g[123, 1],43},{g[124, 1],78}}

Of course I could make it manual but I have many many lists or matrix as you can call them.

My attempt was as follows:

Do[Print[ReplacePart[a1, {i, 1} -> g[a1[[i, 1]], 1]]], {i, 1, 3}]
  {{100,43},{123,54},{124,78}}
{{1,43},{12300,54},{124,78}}
{{1,43},{123,54},{12400,78}}

Almost made it, but a just need the elements of the diagonal matrix if you have another program or another way to operate on each element but not in all i would appreciate it a lot.

In conclusion I want to opearate in one subelements of each element generating a new list not remplacing the existent.

Greetings.

POSTED BY: Charls Mejía
3 Replies
Posted 10 years ago

Thank you very much guys, I' have completed what I wanted and I succeeded by the way of Peter Fleck but applying a function on it:

In[945]:= c2na

Out[945]= {{3.89323, 292.79}, {3.86742, 302.68}, {3.84187, 
  312.64}, {3.8158, 322.62}, {3.79076, 332.58}, {3.76623, 
  342.49}, {3.74067, 352.32}, {3.68726, 373.33}, {3.64484, 391.31}}

c2nag = {pgL[#[[1]], 367.29], #[[2]]} & /@ c2na;

In[946]:= c2nag

Out[946]= {{1.42995, 292.79}, {1.42047, 302.68}, {1.41108, 
  312.64}, {1.40151, 322.62}, {1.39231, 332.58}, {1.3833, 
  342.49}, {1.37391, 352.32}, {1.3543, 373.33}, {1.33871, 391.31}}
POSTED BY: Charls Mejía

I need to operate the first subelement of each element for example if I want to multiply by two,

another possibility

    a1[[All, 1]] *= 2;
   (*a1 now is {{2, 43}, {246, 54}, {248, 78}} *)

And if its possible to aply a function to the first subelement of each element.

   a1[[All, 1]] = g[#, 1] & /@ a1[[All, 1]];
   (*a1 now is {{g[1, 1], 43}, {g[123, 1], 54}, {g[124, 1], 78}} *)
POSTED BY: Nasser M. Abbasi
Posted 10 years ago

One possible approach:

In[2]:= {2 #[[1]], #[[2]]} & /@ a1

Out[2]= {{2, 43}, {246, 54}, {248, 78}}

For this you might want to review Map and pure functions:

http://reference.wolfram.com/mathematica/ref/Map.html

http://reference.wolfram.com/mathematica/howto/WorkWithPureFunctions.html

Hope this helps.

POSTED BY: Peter Fleck
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract