创建索引 MATLAB
Create Index MATLAB
我对尝试成功做到这一点很生气。我在矩阵 size(A) = [100 612]
中有数据 A
。列数据由 51 个站点以 12 个月为一组,即总共 612 个列。
我需要为此序列中的 select 列创建索引; 1:51:612
,然后是 2:51:612
,以此类推,直到 51:51:612
。最终数组应该是一个 100 行乘 612 列的矩阵,序列为
Row 1: Columns 1-12=(1,52,103,154,205,256,307,358,409,460,511,562)
Columns 13-24=(2,53,104,155,206,257,308,359,410,461,512,563)
...
etc to the end of the first row with the last 12 columns with these numbers
Columns 601-612=(51,102,153,204,255,306,357,408,459,510,561,612).
然后重复100次得到100行。我需要将其用作提取或重新排序上面给出的 A
中的原始数据的逻辑索引。
以下代码应该有效:
months = 12;
sites = 51;
idx = 1:sites:months*sites; %// get array [1,52,103,...,562]
final_idx = bsxfun(@plus,idx',[0:sites-1]); %'//add offsets to idx
final_idx = final_idx(:)'; %'//get array elements in a row vector
A_new = A(:,final_idx); %// rearrange columns
的单行代码
out = A(:,reshape(permute(reshape(1:612,51,[]),[2 1 3]),1,[]));
或者您可以使用 transpose
来避免 permute
out = A(:,reshape(reshape(1:612,51,[]).',1,[]));
我对尝试成功做到这一点很生气。我在矩阵 size(A) = [100 612]
中有数据 A
。列数据由 51 个站点以 12 个月为一组,即总共 612 个列。
我需要为此序列中的 select 列创建索引; 1:51:612
,然后是 2:51:612
,以此类推,直到 51:51:612
。最终数组应该是一个 100 行乘 612 列的矩阵,序列为
Row 1: Columns 1-12=(1,52,103,154,205,256,307,358,409,460,511,562)
Columns 13-24=(2,53,104,155,206,257,308,359,410,461,512,563)
...
etc to the end of the first row with the last 12 columns with these numbers
Columns 601-612=(51,102,153,204,255,306,357,408,459,510,561,612).
然后重复100次得到100行。我需要将其用作提取或重新排序上面给出的 A
中的原始数据的逻辑索引。
以下代码应该有效:
months = 12;
sites = 51;
idx = 1:sites:months*sites; %// get array [1,52,103,...,562]
final_idx = bsxfun(@plus,idx',[0:sites-1]); %'//add offsets to idx
final_idx = final_idx(:)'; %'//get array elements in a row vector
A_new = A(:,final_idx); %// rearrange columns
out = A(:,reshape(permute(reshape(1:612,51,[]),[2 1 3]),1,[]));
或者您可以使用 transpose
permute
out = A(:,reshape(reshape(1:612,51,[]).',1,[]));