merge data and generate w matrix