Matrix Indexing in the double for loops
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
the cyclist
on 2 Dec 2015
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 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!