Hello.Here's my code.I want to improve it's efficiency. Therefore,I try to vectorize this for loop in this code.Hope anyone who mastering Matlab can help me solve this problem.Thank you all.
K = 2;
M = 3;
b = zeros(K,1);
A = [1,1,1;
1,1,2;
1,2,1;
1,2,2;
2,1,1;
2,1,2;
2,2,1;
2,2,2];
C = num2cell(A);
for i = 1:M
for j = 1:K^M
for q = 1:K % 1 --> [1;0;...;0] 2 --> [0;1;0;0...;0] K --> [0;0;0;...;1]
if C{j,i} == q
b(q,1) = 1;
C(j,i) = {b};
end
end
b = zeros(K,1);
end
end

 Respuesta aceptada

Stephen23
Stephen23 el 7 de Jun. de 2018

0 votos

K = 2;
S = size(A);
[R,C] = ndgrid(1:S(1),1:S(2));
Z = zeros([K,S]);
Z(sub2ind([K,S],A,R,C)) = 1;
out = permute(num2cell(Z,1),[2,3,1]);

Más respuestas (0)

Categorías

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

Etiquetas

Preguntada:

el 7 de Jun. de 2018

Respondida:

el 7 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by