0
for i=1:1:4 
    T=[(cos(x(i)))^2 (sin(x(i)))^2 2*(cos(x(i)))*(sin(x(i)));(sin(x(i)))^2 (cos(x(i)))^2 -2*(cos(x(i)))*(sin(x(i))) ;-(cos(x(i)))*(sin(x(i))) (cos(x(i)))*(sin(x(i))) (cos(x(i)))^2-(sin(x(i)))^2 ;];
    XXXXX=inv(T)*Qq*R*T*inv(R);
end

I want to name XXXXX according to i; I mean that when i=1 is running, The XXXXX will be variable Q1,and i=2 will be Q2, and on and on.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Guy Balas
  • 98
  • 3
  • 10
  • 1
    I think you'll hve to use `eval()` for something like this. – AnonSubmitter85 Jan 17 '16 at 13:34
  • 1
    Do you have a specific reason to do this insteand of putting it in an array `Q{i}`? – Sam Segers Jan 17 '16 at 13:38
  • 1
    You do not want these names. This is called using "Dynamic variable naming" and is considered [very bad practise](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170). See the linked post for more info on why, and see Daniel's answer for the proper way of storing variables. – Adriaan Jan 17 '16 at 13:39
  • @AnonSubmitter85 While you can actually do this for performing what is exactly asked, it is a very bad idea to give such an answer for at least two reasons: first, the idea of the original poster is probably not the best and we have to explain the right way to do it; second, because using `eval()` isn't a good idea (unless we perfectly know why we are using it which wouldn't probably be the case here). – Thomas Baruchel Jan 17 '16 at 13:49

1 Answers1

2

It is possible but not recommended to use variable names Q1 Q2 Q3. The link both explains why it is not recommended and how to implement it.

Instead, use a cell array to store your results:

n=4; % or probably better n=numel(x)
Q=cell(n,1);
for i=1:1:4; 
    T=[(cos(x(i)))^2 (sin(x(i)))^2 2*(cos(x(i)))*(sin(x(i)));(sin(x(i)))^2 (cos(x(i)))^2 -2*(cos(x(i)))*(sin(x(i))) ;-(cos(x(i)))*(sin(x(i))) (cos(x(i)))*(sin(x(i))) (cos(x(i)))^2-(sin(x(i)))^2 ;];
    Q{i}=inv(T)*Qq*R*T*inv(R);
end
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 1
    I plan on doing that but It gave me an error that Vector could not be a Matrix 3X3. Instead of That I created a 3d Matrix – Guy Balas Jan 17 '16 at 13:48
  • If a 3d Matrix is possible, it is the better because faster choice. I was not sure if the result of each iteration is a matrix of the same size, thus suggested a more general solution which applies in any case. – Daniel Jan 17 '16 at 13:52