I am currently doing a project Euler problem for Pythagorean triplets. I have decided to use matrix transformation to produce my results. It takes a single set of triplets and transforms them with three matrix to produce three new sets triplets, each of these in turn can then be passed through the matrix to produce further triplets, creating a tree of results.
Where I am stuck is how do I store each set of triplets in my pre-allocated array. I need to step incrementally through the array to pick up my new triplets to be transformed and produce three answers that need storing. Do I use two counters, to me this feels messy and inelegant. Is there another way I need to be looking at storing the answers in the array. Advice always gratefully received, thanks.
As requested some code to show what I would like to achieve.
function PyTout = PyTrip(PyTin)
PyMx = zeros(4,1000);
PyMx(1:4,1) = [0 3 4 5 ];
TripTrans1 = [-1 2 2; -2 1 2; -2 2 3];
TripTrans2 = [1 2 2; 2 1 2; 2 2 3];
TripTrans3 = [1 -2 2; 2 1 2; 2 -2 3];
for i = 1:(length(PyMx)/3)
TempMx1 = PyMx(2:4,i);
TempMx2 = TripTrans1 * TempMx1;
PyMx(1:4,2) = [i TempMx2(1:1) TempMx2(2:2) TempMx2(3:3)];
TempMx3 = TripTrans2 * TempMx1;
PyMx(1:4,3) = [i TempMx3(1:1) TempMx3(2:2) TempMx3(3:3)];
TempMx4 = TripTrans3 * TempMx1;
PyMx(1:4,4) = [i TempMx4(1:1) TempMx4(2:2) TempMx4(3:3)];
I could implement a second counter to increment each time I perform a TMx1, TMx,2 TMx3 calculation. But I am asking is there a more efficient way to do this and load the data into the matrix. to me I seem to be typing out a lot of long hand, is there a better, more effitiant or preffered way I should do this. I am not just trying to find the ans to the problem (I could look at wiki page of Py triplets and get excel to sum them and give me an ans) I am also trying to improve my programing and how I think about it. Thanks for your time.