matlab get matrix values into a variable in for loop

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.

 Respuesta aceptada

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
q = 1, aA = anothernot, bB = anotherstr, cC = thisisastr q = 2, aA = anothernot, bB = thisisastr, cC = anotherstr q = 3, aA = anotherstr, bB = anothernot, cC = thisisastr q = 4, aA = anotherstr, bB = thisisastr, cC = anothernot q = 5, aA = thisisastr, bB = anothernot, cC = anotherstr q = 6, aA = thisisastr, bB = anotherstr, cC = anothernot

3 comentarios

Thank you, I would like to ask with the same matrix in mind, could you see one more level of the problem, I can explain here:
I would like to compare the value of aA(for instance) with a string say 'A' which could be line A='sfdgkl'.
However I just get false or '0' values out of this comparison. I have tride strcmp, and isequel functions for matter.
An example could be
if isequal(aA, A)
disp('hello');
elseif isequal(bB, A)
disp('hello2');
elseif isequal(cC, A)
disp('hello3');
end
but
I only get a logical '0' out of this and the if-statement does not do what I'm looking for.
The only output is:
ans =
logical
0
however if I test:
if isequal('hello', 'hello')
disp('hello');
end
the output is a logical 1 or true:
ans =
logical
1
So I understand that the problem could be reading the value stored in the matrix element. But would there be any idea how to extract that value and be able to compare it with a string?
*If I write:
aA
I will get output the string which is stored in it, this might help to know the challenge better? So there is a string in aA and a string in A, but it looks like when I compare it does not compare the string inisde aA with A.
Using if does not change ans
What it was supposed to do was not to change ans
I would like to compare aA which is an element from the matrix, with A which is the string shown above, and if they are alike, print a number as a part of a sentece to the screen. It is this combination which gives an error as:
Unrecognized function or variable 'A'.
have also tried:
A='thisisastr'; B='anotherstr'; C='anothernot';(these are strings and will be string to the end)
aA='anothernot'; bB='thisisastr'; cC='anotherstr';(this is actually the permutated matrix shown in the start of the post)
QQ='something else';
if strcmp(aA,A)
W=1;
elseif strcmp(aA,B)
W=2;
elseif isequal(aA, C)
W=3;
end
if strcmp(bB,A)
U=1;
elseif strcmp(bB,B)
U=2;
elseif strcmp(bB,C)
U=3;
end
if strcmp(cC,A)
Z=1;
elseif strcmp(cC,B)
Z=2;
elseif strcmp(cC,C)
Z=3;
end
fprintf('The house is: %d%d%d %s',W, U, Z, QQ);
Unrecognized function or variable 'W'.
(which works well on its own, but in combination with the matrix it gives an error as I above)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Sept. de 2021

Comentada:

el 4 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by