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

1 Ansicht (letzte 30 Tage)
SammyJoe
SammyJoe am 18 Okt. 2019
Kommentiert: SammyJoe am 18 Okt. 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)

Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 18 Okt. 2019
Bearbeitet: KALYAN ACHARJYA am 18 Okt. 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 Kommentare
SammyJoe
SammyJoe am 18 Okt. 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by