How do I save the matrices made in a loop and save them to a matrix?

1 visualización (últimos 30 días)
SammyJoe
SammyJoe el 18 de Oct. de 2019
Comentada: SammyJoe el 18 de Oct. de 2019
This is what I have and I am getting the final answer to be 180 when it should be 16; I believe my problem is at the circular shift part, I need c to be a matrix with all of the circular shifts in it so I can remove the circular shifts from the total
%Create a vector with the beads where a number is assigned to each color
v=[1 1 2 2 3 4];
% 1=red
% 2=blue
% 3=yellow
% 4=green
% Determine total number of beads and all possible permutations
n=numel(v);
p=perms(v);
% Take into account colors with >1 beads
p2=unique(perms(v),'rows');
% Generate flips of all permutations
f=fliplr(p2);
% Generate circular shifts for all permutations
for i=0:n
c=circshift(p2,i);
end
% Combine the three matrices into one and remove duplicate rows
combined=[p2;f;c];
final=unique(combined,'rows');
configurations=size(final,1);
fprintf('The number of possible configurations is %d\n',configurations)

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 18 de Oct. de 2019
Editada: KALYAN ACHARJYA el 18 de Oct. de 2019
How do I save the matrices made in a loop and save them to a matrix?
Use cell array
In your case:
c=cell(1,n); % Preallocation
for i=1:n
c{i}=circshift(p2,i);
end
Now you can call any matrices using c{1}, c{2}, ....etc
  3 comentarios
SammyJoe
SammyJoe el 18 de Oct. de 2019
No more error, but my final answer is coming out as 180 and it is supposed to be 16, why is this not working?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center 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