Matrix Indexing in the double for loops

2 次查看(过去 30 天)
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

采纳的回答

the cyclist
the cyclist 2015-12-2
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by