我正在寻找一个矢量化解决方案!
我有两个数组,例如:
idx=[1 1 1 2 2 3 3 4 4 4 5 5 6 7 8 8 8 9 9]; %%//Integers,sorted
val=[1 4 8 2 5 3 9 1 4 8 2 5 6 7 1 4 8 3 9]; %%//respective Values (could be anything)
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个单元格数组,其中包含由其idx相应值指定的元素val.所以结果应该是[9x1] cell:
[1 4 8]
[2 5]
[3 9]
[1 4 8]
[2 5]
[6]
[7]
[1 4 8]
[3 9]
Run Code Online (Sandbox Code Playgroud)
我知道我可以循环从1到9的值并使用horzcat,而idx等于我的循环索引,但我正在寻找一个矢量化的解决方案.原因是,我试图将问题的循环解决方案更改为矢量化解决方案,但我被困在这里
注意:如果你能指出没有'eval'的解决方案,那就太棒了!如果没有,我也会感激:-)好吧,我有一个cell(Var_s),它在第一行字符串和第二行矩阵中:
clc
clearvars
fclose all
L=[11 22 33 44];
M=[1 2 3];
N=[101 102 103 104 105, 95 96 97 98 99];
Var_s=cell(2,3);
Var_s(1,1:3)={'Rn', 'LE', 'O'}; %// The strings are all different and were not created in a loop.
Var_s(2,1:3)={L, M, N}; %// No correlation is possible.
%//Than I delete all variables because I can work with the cell (Var_s{2,:})
%//to execute some computations
clearvars L M N
%//Now I want to save the values which are stored …Run Code Online (Sandbox Code Playgroud)