It could be a zinc deficiency, that causes mental problems. Zinc needs B6 to function properly. So make sure your B6 is high enough. You can tell your B6 is high enough when you can recall your dreams in the morning.
Don't take my word, start reading.
Magnesium and B6 in the morning and at lunch, take zinc in the evening.
To test for zinc deficiency you can do a taste test. (search for zinc deficiency taste test)