在Matlab中使用矢量元素作为索引

在Matlab中使用矢量元素作为索引,matlab,for-loop,Matlab,For Loop,我举了一个毫无意义的小例子,让你们了解我的问题: 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不喜欢我使用i(1)作为“for”循环的索引: Error: File: prova.m Line: 4

我举了一个毫无意义的小例子,让你们了解我的问题:

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不喜欢我使用i(1)作为“for”循环的索引:

Error: File: prova.m Line: 4 Column: 6
Unbalanced or unexpected parenthesis or
bracket.
如果我将I(1)替换为j,则一切正常。 不能使用数组单元格存储不同循环的索引吗

我必须做一些事情,比如:

...
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
我想用以下内容取代它:

...
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
目前,这是我的解决办法:

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;

i
是循环的索引变量(在每次迭代中,
i
按顺序从
1:n
获取一个值。将此变量设置为
j
(或任何变量名称)将为每次迭代分配一个值

i(1)
在您的示例中是值0,所以这就像说
0=1

只需在循环内部使用
stored_val=i
即可获得当前值。在嵌套过程中,每个外部循环变量都存在于所有内部循环中。因此,在最内部的循环中
i_all=[i1,i2,i3]
并将其作为输入传递给函数


此外,避免将
i
j
用作MATLAB中的任何类型的变量(请参见:)

你期望的结果是什么?我有一些嵌套的函数,在它们里面我调用了另一个函数,我必须将迭代的实际索引传递给它。我会用它来传递向量i,而不是单个的i1,i2,i3…我不太明白你的意思。你能分享更多的代码吗?对于i5=1:nChannel对于i6=1:nChannel forI7=1:nChannel for i8=1:nChannel for i9=1:nChannel for i10=1:nChannel A=A函数(para,true,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10);您可以在答案中发布这些代码。这样会更清晰。