gather data from different sections of each column

1 回表示 (過去 30 日間)
Christopher
Christopher 2015 年 11 月 18 日
コメント済み: Christopher 2015 年 11 月 23 日
Consider the code:
totmods = 10000;
numselect = 1000;
A = rand(100,totmods); % numbers
B = randi(90,10,1000); % start row index
C = randi(totmods,numselect,1); % column index
D = A(B:B+9,C);
The resulting matrix D should be the 10x1000 matrix wherein each column is a selection of 10x1 data starting from the index B for each column in A. However, the code does not work correctly. The code uses only B(:,1) as the row index for all the elements gathered from A in the line "D = A(B:B+9,C);"
How can this be corrected without using a loop?
  1 件のコメント
Guillaume
Guillaume 2015 年 11 月 18 日
編集済み: Guillaume 2015 年 11 月 18 日
The code uses only B(:,1) is slightly incorrect. The code actually only uses B(1, 1) because that's the documented behaviour of the colon operator:
If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1)
I have no idea which values of B (considering it is 10x1000 matrix) you're trying to use for each row of your D output.

サインインしてコメントする。

採用された回答

Thorsten
Thorsten 2015 年 11 月 18 日
You only need numelselect values of rows:
B = randi(90,numselect,1);
Then use
for i = 1:numel(B), D(:,i) = A(B(i):B(i)+9, C(i)); end
  3 件のコメント
Thorsten
Thorsten 2015 年 11 月 18 日
編集済み: Thorsten 2015 年 11 月 18 日
Try
D = A(bsxfun(@plus, bsxfun(@plus, B, 0:9), (C-1)*size(A,1))');
This computes the linear indices into A based on B and C.
Christopher
Christopher 2015 年 11 月 23 日
This has worked well, thanks

サインインしてコメントする。

その他の回答 (2 件)

Guillaume
Guillaume 2015 年 11 月 18 日
totmods = 10000;
numselect = 1000;
A = reshape(1:100*totmods, 100, totmods);
B = randi(size(A, 1) - 9, numselect, 1); %column vector
C = randi(totmods, 1, numselect); %row vector to make it easier later
rowmatrix = bsxfun(@plus, B, 0:9)'; %matrix of row indices
D = A(sub2ind(size(A), rowmatrix, repmat(C, 10, 1)))

Stephen23
Stephen23 2015 年 11 月 18 日
編集済み: Stephen23 2015 年 11 月 18 日
Preallocate the output before allocating to it:
As numselect gets larger this will have a very significant effect on the calculation speed. Try something this:
B = randi(90,numselect,1);
D = nan(10,numel(B));
for k = 1:numel(B)
D(:,k) = A(B(k):B(k)+9, C(k));
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by