For loop 使用步骤1在0到5的向量中创建所有可能的组合?

For loop 使用步骤1在0到5的向量中创建所有可能的组合?,for-loop,For Loop,我有这个MATLAB代码,它从向量U=[0]开始,到U=[5]结束。我的目标是通过步骤1找到U=[0]和U=[5]之间所有可能的向量 当然,这个很简单 U = [0 0 0]; step = 1; max_value = 5; min_value = 0; for j = min_value:step:max_value for k = min_value:step:max_value for l = min_value:step:max_value sprintf(

我有这个MATLAB代码,它从向量
U=[0]
开始,到
U=[5]
结束。我的目标是通过步骤
1
找到
U=[0]
U=[5]
之间所有可能的向量

当然,这个很简单

U = [0 0 0];
step = 1;
max_value = 5;
min_value = 0;


for j = min_value:step:max_value
  for k = min_value:step:max_value
    for l = min_value:step:max_value
      sprintf("%f,%f,%f", j, k, l)
    end
  end
end
输出为:

ans = 0.000000,0.000000,0.000000
ans = 0.000000,0.000000,1.000000
ans = 0.000000,0.000000,2.000000
ans = 0.000000,0.000000,3.000000
ans = 0.000000,0.000000,4.000000
ans = 0.000000,0.000000,5.000000
ans = 0.000000,1.000000,0.000000
ans = 0.000000,1.000000,1.000000
ans = 0.000000,1.000000,2.000000
ans = 0.000000,1.000000,3.000000
ans = 0.000000,1.000000,4.000000
ans = 0.000000,1.000000,5.000000
ans = 0.000000,2.000000,0.000000
ans = 0.000000,2.000000,1.000000
ans = 0.000000,2.000000,2.000000
ans = 0.000000,2.000000,3.000000
ans = 0.000000,2.000000,4.000000
ans = 0.000000,2.000000,5.000000
ans = 0.000000,3.000000,0.000000
ans = 0.000000,3.000000,1.000000
ans = 0.000000,3.000000,2.000000
ans = 0.000000,3.000000,3.000000
ans = 0.000000,3.000000,4.000000
ans = 0.000000,3.000000,5.000000
ans = 0.000000,4.000000,0.000000
ans = 0.000000,4.000000,1.000000
ans = 0.000000,4.000000,2.000000
ans = 0.000000,4.000000,3.000000
ans = 0.000000,4.000000,4.000000
ans = 0.000000,4.000000,5.000000
ans = 0.000000,5.000000,0.000000
ans = 0.000000,5.000000,1.000000
ans = 0.000000,5.000000,2.000000
ans = 0.000000,5.000000,3.000000
ans = 0.000000,5.000000,4.000000
ans = 0.000000,5.000000,5.000000
ans = 1.000000,0.000000,0.000000
ans = 1.000000,0.000000,1.000000
ans = 1.000000,0.000000,2.000000
ans = 1.000000,0.000000,3.000000
....
....
....
ans = 5.000000,5.000000,5.000000
但是假设我们有向量
U
n
元素。然后不能使用多个for循环。相反,我需要使用这样的东西:

  for i = 1:length(U)
    for j = min_value:step:max_value
      U(i) = j;
    end
  end
但这也行不通。您对如何为任意向量
U
创建此循环有何建议


你使用什么语言并不重要。我刚刚用MATLAB试过,但C也很好用。

蛮力解决方案可以是:

N = 3;
min_value = 0;
max_value = 5;
step = 1;
U = min_value.*ones(1, N);

idx = length(U); % index of the element to be changed
while (1)
    if (U(idx) <= max_value-step)
        U(idx) = U(idx) + step; % increase the element
        U % display
    elseif (sum(U) == N*max_value)
        break; % all elements are maxed, get out
    else
        % current index is maxed, so move left
        idx = find(U <= max_value - step, 1, 'last'); % get the rightmost element that is not maxed
        U(idx) = U(idx) + step; % increase it
        U(idx+1:end) = min_value; % reset all elements to the right of it
        idx = length(U); % restart from the rightmost element
    end
end
N=3;
最小值=0;
最大_值=5;
步骤=1;
U=最小值。*个(1,N);
idx=长度(U);%要更改的元素的索引
而(1)

如果(U(idx)这里是我的解决方案ic代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <math.h>

void increment(uint8_t* index, float* u, uint8_t* elements, float* step, float* max_value, float* min_value){
    if(*index >= *elements - 1)
        return;
    *index = *index + 1;
    u[*index] += *step;
    if(u[*index] > *max_value){
        u[*index] = *min_value;
        increment(index, u, elements, step, max_value, min_value);
    }
    *index = *index - 1;
}

void print_vector(float* A, int row, int column) {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            printf("%0.18f\t", *(A++));
        }
        printf("\n");
    }
    printf("\n");
}

int main() {

    // Initial parameters
    float u[3] = {0,0,0};
    uint8_t elements = 3;
    float min_value = 0;
    float max_value = 20;
    float step = 1;

    // Start clock
    clock_t start, end;
    float cpu_time_used;
    start = clock();

    // Do the process
    uint8_t index = 0;
    for(uint32_t i = 0; i < powf(max_value - min_value + 1, elements); i++){
        for(float j = min_value; j <= max_value; j += step){
            u[index] = j;
            print_vector(u, 1, elements);
        }
        increment(&index, u, &elements, &step, &max_value, &min_value);
    }

    // Compute the speed
    end = clock();
    cpu_time_used = ((float) (end - start)) / CLOCKS_PER_SEC;
    printf("\nTotal speed  was %f\n", cpu_time_used);

    return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括
无效增量(uint8_t*索引、浮点*u、uint8_t*元素、浮点*步长、浮点*最大值、浮点*最小值){
如果(*索引>=*元素-1)
返回;
*指数=*指数+1;
u[*索引]+=*步进;
如果(u[*索引]>*最大值){
u[*索引]=*最小值;
增量(索引、u、元素、步长、最大值、最小值);
}
*指数=*指数-1;
}
无效打印向量(浮点*A,整数行,整数列){
对于(int i=0;i对于(float j=min_value;j)你所说的任意向量是什么意思?@SardarUsama任意向量介于$N$和$M$之间,但仍然适用。例如,任意值可以是1、2100500、40、5,但不是无限大或非常大。
Us=[0 0 0];step=[1 1 1];Ue=[5 5];
A=arrayfun(@(A、b、c)冒号(a,b,c),Us,step,Ue,'un',0);
U=fliplr(combvec(a{:})。
类似的东西?@SardarUsama这些是任意向量是的。好吗?‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍ ‍‍‍‍‍‍