2

The following saves floating values of a matrix into textfiles

numpy.savetxt('bool',mat,fmt='%f',delimiter=',')

How to save a boolean matrix ? what is the fmt for saving boolean matrix ?

6
  • 1
    What do you want the output to look like? Commented Jun 19, 2012 at 11:34
  • 1
    Related: stackoverflow.com/questions/4515373/… Commented Jun 19, 2012 at 11:43
  • 3
    If the main aim is to load the file back, why do you want to use a text file? The binary representation used by numpy.load() and numpy.save() will be much more efficient. Commented Jun 19, 2012 at 11:45
  • 3
    Do you need np.savetxt/np.loadtxt? How about np.save/np.load? Commented Jun 19, 2012 at 11:45
  • 1
    Why not try '%d', In python, True/False are ints, so I don't see any reason why that wouldn't work. Commented Jun 19, 2012 at 12:01

1 Answer 1

3

Thats correct, bools are integers, so you can always go between the two.


import numpy as np
arr = np.array([True, True, False, False])
np.savetxt("test.txt", arr, fmt="%5i")

That gives a file with 1 1 0 0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.