Borrar filtros
Borrar filtros

Matrix Indexing in the double for loops

2 visualizaciones (últimos 30 días)
Homayoon
Homayoon el 2 de Dic. de 2015
Respondida: the cyclist el 2 de Dic. de 2015
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

Respuesta aceptada

the cyclist
the cyclist el 2 de Dic. de 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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by