Hi, I have a problem. This is my program text.
for i=1:614
if (x1(i,1) == 0)
p=[1 0 0 0 0 0 0 0 0 0 0 0];
elseif ((x1(i,1) > 0) && (x1(i,1) <= 10000))
p=[0 1 0 0 0 0 0 0 0 0 0 0];
elseif ((x1(i,1) > 10000) && (x1(i,1) <= 20000))
p=[0 0 1 0 0 0 0 0 0 0 0 0];
elseif ((x1(i,1) > 20000) && (x1(i,1) <= 30000))
p=[0 0 0 1 0 0 0 0 0 0 0 0];
elseif ((x1(i,1) > 30000) && (x1(i,1) <= 40000))
p=[0 0 0 0 1 0 0 0 0 0 0 0];
elseif ((x1(i,1) > 40000) && (x1(i,1) <= 50000))
p=[0 0 0 0 0 1 0 0 0 0 0 0];
elseif ((x1(i,1) > 50000) && (x1(i,1) <= 60000))
p=[0 0 0 0 0 0 1 0 0 0 0 0];
elseif ((x1(i,1) > 60000) && (x1(i,1) <= 70000))
p=[0 0 0 0 0 0 0 1 0 0 0 0];
elseif ((x1(i,1) > 70000) && (x1(i,1) <= 80000))
p=[0 0 0 0 0 0 0 0 1 0 0 0];
elseif ((x1(i,1) > 80000) && (x1(i,1) <= 90000))
p=[0 0 0 0 0 0 0 0 0 1 0 0];
elseif ((x1(i,1) > 90000) && (x1(i,1) <= 100000))
p=[0 0 0 0 0 0 0 0 0 0 1 0];
elseif (x1(i,1) > 100000)
p=[0 0 0 0 0 0 0 0 0 0 0 1];
else
end
D=p*P
end
x1 is [614x1] vector.
P is [12x12] matrix.
So with this program my answer is vector D, given 614 times (because x1 have 614 elements). But I need that those vectors would be combine into [614x12] one matrix.
How can I do that? it very urgent.

 Respuesta aceptada

James Tursa
James Tursa el 22 de Mayo de 2015
Editada: James Tursa el 22 de Mayo de 2015
Assuming none of the x1 values are negative, which from the looks of your code seems to be an assumption you made.
D = zeros(614,12);
for i=1:614
:
D(i,:) = p * P;
But it looks like you are simply picking off rows of P depending on the value of x1. This could be vectorized. E.g.,
x = ceil(x1/10000) + 1;
x(x>12) = 12;
D = P(x,:);

3 comentarios

Akvile Dumbliauskaite
Akvile Dumbliauskaite el 22 de Mayo de 2015
Thank you so much :) I get the matrix. But there is a little problem, I get that matrix 614 times :) I need that matrix just one time.
James Tursa
James Tursa el 22 de Mayo de 2015
Editada: James Tursa el 22 de Mayo de 2015
Did you replace the D=p*P line with D(i,:)=p*P? And put a semi-colon at the end of the line to suppress the screen output?
Akvile Dumbliauskaite
Akvile Dumbliauskaite el 22 de Mayo de 2015
Oh sorry, it gives a good answer :) Thank you :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by