matlab get matrix values into a variable in for loop
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nicle Davidson
le 4 Sep 2021
Commenté : Nicle Davidson
le 4 Sep 2021
I have three values as:
A='thisisastr';
B='anotherstr';
C='anothernot';
I would like to first permutate the values like this:
house = {A, B, C};
P = perms(house);%which will give me a 3*6 matrix
Then I would like to use these, like in a for-loop three by three, each of them in one variable:
for q=1:size(P,1) %not sure if it should be 1 or 3 here
aA=P(q,1);
bB=P(q,2);
cC=P(q,3);
%do a lot of things with aA and bB and cC and come back and take the next row values and do the same things with them and at the end the third row values (here is the rest of whole code of mine, things I need to do with the strings which are in each of the matrix values)
end
The problem is when I don't have this for-loop I don't get any error from the code itself, but adding this for-loop to change the place of A and B and C or actually get the values of next row in matrix into those variables aA, bB, cC, then I get this error:
Index exceeds the number of array elements (1).
I even chaneged the
for q=1:size(P,1)
to
for q=1:length(P)
but still same problem.
without this for-loop I don't get index error from the program, but then I can not check A,B,C in all positions. (like A,B,C and A,C,B, and B,A,C and B,C,A and C,B,A and C,A,B)
Appreciate any help.
0 commentaires
Réponse acceptée
Walter Roberson
le 4 Sep 2021
A='thisisastr';
B='anotherstr';
C='anothernot';
house = {A, B, C};
P = perms(house);%which will give me a 3*6 matrix
for q=1:size(P,1) %not sure if it should be 1 or 3 here
aA = P{q,1};
bB = P{q,2};
cC = P{q,3};
fprintf('q = %d, aA = %s, bB = %s, cC = %s\n', q, aA, bB, cC);
end
3 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!