• 100% Satisfaction Guarantee
Zabo04, Programming Enthusiast
Category: Programming
Satisfied Customers: 283
Experience:  C/C++, python, perl, excel, matlab/octave, java, and R experience. Genetic algorithm, vision, GUIs.
58597962
Zabo04 is online now

# Can you help with Python i.e. resolving some queries where

### Resolved Question:

can you help with Python i.e. resolving some queries where conditional statements are not working as I planned?
Submitted: 1 year ago.
Category: Programming
Customer: replied 1 year ago.
Hi. I have a non square matrix where each row contains a range of different numbers. How do I get Python to iterate through these rows to pick the smallest number in each row. Also, I have a vector of random numbers i.e [2,4,5,6,7,4,6,3] and these show which row in the matrix I want the process to iterate to. i.e the first iteration will be on row 2 and will find the smallest number. The next iteration will be on row 4 to find the smallest number. Also, so for example there are two instance where the 6th row is to be iterated. How do I get it to only ever use a value once in each row. I.e find the smallest number in the first iteration on row 6 then find the second smallest number in the next iteration over row 6?
Customer: replied 1 year ago.
I have also a padded matrix that makes it square and all padded values are 'nAn'
Expert:  Zabo04 replied 1 year ago.

I am working on your question now and I should have something in a little bit that serves as an example.

Customer: replied 1 year ago.
Hi, thanks. If it helps it is an algorithm of the greedy nature but It to eliminate certain elements once they have already been used
Expert:  Zabo04 replied 1 year ago.

Just use and replace with NaN.

Customer: replied 1 year ago.
Oh OK thanks. What is the time scale on your example being ready?
Expert:  Zabo04 replied 1 year ago.

Numpy isn't installing on my windows box, so I have to find another way to solve the problem.

Expert:  Zabo04 replied 1 year ago.

Try this out and let me know how it works for you. I had to name the file .txt to attach it, but just change the extension to .py and run.

Customer: replied 1 year ago.
Thanks this seems to work. One question, how do I define my own matrix i.e not random values but type out my own matrix?
Customer: replied 1 year ago.
Sorry, I have defined my own matrix now. I have a matrix ([[1,2,3,4,5,6,7,8,9],[1,2,4,7,2,4,5,6,34],[3,4,6,1,7,88,2,5,4],[4,8,67,45,55,2,9,5,9]]) and search vector [2, 2, 3, 2, 0] and im getting wrong results:Row 2
[3, 4, 6, nan, 7, 88, 2, 5, 4]
Min: 1Row 2
[3, 4, 6, nan, 7, 88, 2, 5, 4]
Min: 2Row 3
[4, 8, 67, 45, 55, 2, 9, 5, 9]
Min: 2Row 2
[3, 4, 6, nan, 7, 88, 2, 5, 4]
Min: 2 <------- This should not be 2. The 2 should have been replaced with nan and min should be 3Row 0
[nan, 2, 3, 4, 5, 6, 7, 8, 9]
Min: 1
Customer: replied 1 year ago.
Sorry for all the questions. I have another one. How would I set all values to 0 in the column that the found value is in. i.e if the minimum value is in column 20, how do i then set all values in column 20 to 0?
Expert:  Zabo04 replied 1 year ago.

Let me look at this. Sorry for the delayed response, I'm in the US on the east coast and I had to call it a night so I could get some sleep.

Expert:  Zabo04 replied 1 year ago.

Okay, I think I answered all of your questions. I updated the comments in the python code, defined your matrix, replaced the column for the minimum value with 0 in all rows. I added printing the updated matrix so that you can see the 0s. I also changed the min function and included a filter argument so it will find the minimum value greater than 0, that why once you have a match when you replace the column values for the match with a 0. I also moved where the row is printed, so instead of seeing the 0 or NaN that was replaced, it shows the row so you can verify it found the minimum value, and then it prints the whole matrix to show the column replacements. Ask more questions if you have them. I attached the file with a .txt again, JustAnswer filters file types and .py is not allowed neither is .cpp or .c or .h or .m, it just gets annoying sometimes.

I think the none NaN replacement the second time was the equality operator on NaN. NaN is nothing so it both is equal and not equal. I have python 2.7.12, which is better, but NaN is still a newer concept to the language, relatively. Numpy handles it, but then you have to have numpy, and it really is better to solve in the base language if possible so that it is more portable and has fewer dependencies. Using 0s and a filter works better and is what you want anyways. Thanks for working with me.