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

1 view (last 30 days)
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)

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 18 Oct 2019
Edited: KALYAN ACHARJYA on 18 Oct 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 Comments
SammyJoe
SammyJoe on 18 Oct 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?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!