0

I made this small meaningless example for you to see my problem:

function a = prova ()
    n=5;
    i = zeros(n,1);
    for i(1) = 1:n %Here is the problem, neither i{1} would work
        disp('hello world');
    end
    a=4;
end

Matlab does not like me using i(1) as my index for the 'for' loop:

Error: File: prova.m Line: 4 Column: 6
Unbalanced or unexpected parenthesis or
bracket.

If I replace i(1) with j, everything works fine. Is not possible to use an array cell to store the indexes of different loops?

I have to do something like:

...
for i5 = 1 : nChannel
    for i6 = 1 : nChannel
    for i7 = 1 : nChannel
        for i8 = 1 : nChannel
        for i9 = 1 : nChannel
            for i10 = 1 : nChannel
            A = aFunction(para, true, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
end %all fors

And i wanted to replace it with:

...
for i(5) = 1 : nChannel
    for i(6) = 1 : nChannel
    for i(7) = 1 : nChannel
        for i(8) = 1 : nChannel
        for i(9) = 1 : nChannel
            for i(10) = 1 : nChannel
            A = aFunction(para, true, i_count, i);
end %all fors

At the moment this is my workaround:

for i5 = 1 : nChannel
i(5)=i5;
for i6 = 1 : nChannel
    i(6)=i6;
        for i7 = 1 : nChannel
        i(7)=i7;
            for i8 = 1 : nChannel
            i(8)=i8;
Francesco
  • 11
  • 4

1 Answers1

0

i is the index variable of the loop (in each iteration i gets a value from 1:n sequentially. Setting this variable to j (or any variable name) will assign a single value it it per iteration.

i(1) is the value 0 in your example, so it's like saying 0=1.

Just use stored_val = i inside the loop to get the current value. In a nested process, each outer-loop variable exists in all the inner-ones. So, inside the inner-most loop do i_all = [i1, i2, i3] and pass this as input to the function.

Also, avoid the use of i and j as any type of variable in MATLAB (See: Using i and j as variables in Matlab)

Community
  • 1
  • 1
gevang
  • 4,994
  • 25
  • 33