?
Solved

values change in python as in REFERENCE????

Posted on 2010-04-07
9
Medium Priority
?
263 Views
Last Modified: 2012-06-27
Sort matrix values by absolute value abs(Lambda1)<abs(Lambda2)
it's not working i should have in the end both -9 and -10 in Lambda2 and -4 and -6 in Lambda1.
the problem is that i am having -4 and -6 in both matrices!!!
from numpy import *

mu1 = array([[1,2,-9],[4,5,-10],[7,8,9]])
mu2 = array([[1,2,-4],[4,5,-6],[7,8,9]])
check=abs(mu1) > abs(mu2)
print check
Lambda1=mu1
Lambda1[check]=mu2[check]
Lambda2=mu2
Lambda2[check]=mu1[check]

Open in new window

0
Comment
Question by:dadadude
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 30023268
I am not sure what are you trying to do, but yes. Any assignment in Python means assigning a reference to the source object.
0
 

Author Comment

by:dadadude
ID: 30024645
i want to switch the values in the matrices. meaning i want to compare every element in both matrices and put the greater one in Lambda2 and smaller in Lambda1.
so if i have:
A =
1 2 3 -11
9 2 4 8
0 9 2 3
B =
1 2 3 8
9 2 4 8
0 9 2 3

i should have in the end :
A =
1 2 3 8
9 2 4 8
0 9 2 3
and
B =
1 2 3 -11
9 2 4 8
0 9 2 3

you can clearly SEE that the values -11 and 8 have been switched in both matrices. that's IT.
0
 
LVL 29

Accepted Solution

by:
pepr earned 1500 total points
ID: 30059499
I do not know what code prints the A and B.  However, the upper code says on the lines 7 and 9:

Lambda1=mu1
...
Lambda2=mu2

This makes Lambda1 identical to mu1 (the same instance via another reference), and Lambda2 identical to mu2

This way, your code can be simplified to -- see below. Does it make sense, now?
from numpy import *

mu1 = array([[1,2,-9],[4,5,-10],[7,8,9]])
mu2 = array([[1,2,-4],[4,5,-6],[7,8,9]])
check=abs(mu1) > abs(mu2)

mu1[check]=mu2[check]
mu2[check]=mu1[check]

Open in new window

0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 29

Assisted Solution

by:pepr
pepr earned 1500 total points
ID: 30059707
In other words, whatever the check is, the elements in mu1 will be set to the same values as the elements in mu2. The the line 8 in the last snippet assigns the same values back to mu2.  I.e. both matrices should contain the same values on the spots determined by check.
0
 

Author Comment

by:dadadude
ID: 30109397
thank you. it's ok i solved the problem. Actually what i want is totally the opposite I want in the end to switch the values in matrix 1 that are greater than the values in matrix 2. I didn't know that python has a reference for r-values where as matlab doesn't. i just modified a little the code.

Thanks anyway
0
 

Author Closing Comment

by:dadadude
ID: 31711811
I SOLVED THAT QUESTION ON MY OWN!!!!!!!!!!!!!!!!!!
0
 

Author Comment

by:dadadude
ID: 30109616
lol sorry they asked me to add a reason for 1000 TIMES lol take care thank you.
0
 
LVL 29

Expert Comment

by:pepr
ID: 30111081
OK. Next time, you can post the answer to your own question and accept it as a solution. Or, if nobody commented your question, you can simply delete it.
0
 

Author Comment

by:dadadude
ID: 30111171
no problem. Thanks anyway. well u didn't help for nothing u deserve points. u helped me in a way.
Thank you.

until next time.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question