필터 지우기
필터 지우기

Matrix Indexing in the double for loops

조회 수: 2 (최근 30일)
Homayoon
Homayoon 2015년 12월 2일
답변: the cyclist 2015년 12월 2일
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