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

조회 수: 1 (최근 30일)
SammyJoe
SammyJoe 2019년 10월 18일
댓글: SammyJoe 2019년 10월 18일
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)

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 10월 18일
편집: KALYAN ACHARJYA 2019년 10월 18일
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
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 10월 18일
I have edited, please check
SammyJoe
SammyJoe 2019년 10월 18일
No more error, but my final answer is coming out as 180 and it is supposed to be 16, why is this not working?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by