Azzera filtri
Azzera filtri

Permutation of specific strings in switch cases and test on what is put in locations (the strings)

3 visualizzazioni (ultimi 30 giorni)
I have called this post for permutation in switch, but I am not sure that it was the right title for the question.
I have three strings such as:
A='abcdh' ;
C='ijklm ';
I have also a matrix of permutated '123' such as U = perms('123');
I run to a for-loop to test all U's
and in that for-loop for each element in U, I need to do something
something is:
I need to for the first element of U, say it is '321',
put A,B,C in three different locations.
location1 = A;
location2 = B;
location3 = C;
and then I need to do a test with what is in these locations, after the test then I need to change what I put in these locations like:
location1 = B;
location2 = C;
location3 = A;
and again do all tests on what is in these locations.
and then after that change what I put in these locations again, do this until all
have participated in the test. After that then I need to do the whole round with the next element in the U matrix.
Say the test is done with '321' then do all that with '312' etc.
I could guess that I should start with a switch such as: (or a for-loop could be better? i guess)
switch U
case ...%the first element of U
%put A,B,C in new locations
%do the test on what is put (those strings) in those locations
% test the next element in U matrix I guess by going to the next case
*The test shall be done on the strings which are defined in the first place.
But some how I need help to set this up
any idea would be appriciated.
  1 Commento
Lawrence il 9 Set 2021
With a switch statement, you only get to run one case. And it's the entire U (not each row) that gets tested against the cases when choosing which one to run.

Accedi per commentare.

Risposta accettata

Lawrence il 9 Set 2021
Modificato: Lawrence il 9 Set 2021
I'd do this with nested for loops, using perms() a second time to go through the locations:
U = perms('123');
locations = perms({A B C})
for i = 1:size(U,1)
for j = 1:size(locations,1)
u = U(i,:);
location1 = locations{j,1};
location2 = locations{j,2};
location3 = locations{j,3};
%Perform test here
  1 Commento
Nicle Davidson
Nicle Davidson il 9 Set 2021
I think this code should work, please let me test, it will take a bit time, but I think this will work, I will come back here and click on accept when tested, and yes, the test will run the same test code. And yes for loop is suited better I see for this case. I will come back and click on accepted when I test it. I appreciate your time and effort.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by