MATLAB: Matrix Indexing in the double for loops

for loopindexing

Dear All,
I am struggling with setting the index for the matrix I defined in my code. I cannot figure how to write the appropriate index.
Okay, Assume there are 50 numbers in the column matrix of K. The objective is to write down the matrix R whose elements are the cross terms of the elements of the matrix K. In other words matrix R would be a column matrix of size (50*49)/2. please note that the order is not important at all. I wrote the code in the following way:
For j=1:49
for t =j+1 : 50
R(???? ,1) = K(j,1)*K(t,1)
end
end
I do not know what should I write for the ???? in the code! Is there any way to index this matrix?
I really do appreciate your helps.
HRJ

Best Answer

One simple way:
% Preallocate the memory:
R = zeros((50*49)/2,1);
row = 0;
for j=1:49
for t =j+1 : 50
row = row+1;
R(row,1) = K(j,1)*K(t,1)
end
end