Search code examples
matlabvariable-names

Naming veriable according to The index of The loop


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.


Solution

  • 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