Indexing to subtract sections of an array
16 ビュー (過去 30 日間)
古いコメントを表示
Hi,
Say you have an 1x80000 row vector and need to subtract by blocks of 64 every so many elements. For example
A = rand(1,80000)
B = A(1:64)-A(81:145)-A(146:210) and so on till you reach the end, which maybe not be 80,000.
I can think of a way to use a loop and construct a series to accomplish this but there must be a vectorized way of doing it. I've used things like
b = a(:,1:end-1) - a(:,2:end);
but I can't find examples where people take blocks of an array and subtract.
Thanks, Charles
0 件のコメント
採用された回答
Adam
2014 年 8 月 15 日
編集済み: Adam
2014 年 8 月 15 日
A = reshape( A, [ 80, numel(A)/80 ] );
A = A(1:64,:);
That is going more off your recent comment rather than your original question which is not very clear on the issue of missing out 16 out of every 80 values. I'm not 100% sure what you want to do after that, but if you just want to subtract every row/column of 64 from the first row then you can just do:
A(:,1) - sum( A(:,2:end), 2 );
0 件のコメント
その他の回答 (3 件)
Nitin
2014 年 8 月 15 日
Suppose you have 20 elements:
Reshape to 4 rows and 5 columns
X = reshape(1:20,5,4)';
% Substract rows
Y = bsxfun(@minus,X,X(1,:));
Michael Haderlein
2014 年 8 月 15 日
Let's bring it down to some more handsome numbers, say a row vector of 17 and you want to have blocks of three.
A=rand(1,17);
a=reshape(A(1:3*fix(length(A)/3)),3,fix(length(A)/3));
B=(a(:,1)-sum(a(:,2:end),2))'
2 件のコメント
Michael Haderlein
2014 年 8 月 16 日
編集済み: Michael Haderlein
2014 年 8 月 16 日
Well, you should clarify what you want: Do want to skip all 16 elements which are following any 64 element block or only the 16 elements which are following the very first 64 element block? Your question and your comment say different things.
In any case, it would have been a good idea to point out in your first question that you want to skip anything at all as this is not mentioned it the text but only appears in the numbers. You'd get the right answer faster if this was mentioned explicitly.
In case you always want to skip 16 elements: Just reshape as shown, the first dimension in the reshaping will then simply be 64+16=80 (thus, replace all the "3" in the reshape line by "80"). Then sum up only the first 64 rows:
B=(a(1:64,1)-sum(a(1:64,2:end),2))';
In case you only want to skip the 16 elements the first time, I'd simply remove them from the array
A(65:80)=[]; %remove this part
or, if you need the complete array in a later part of your code, copy the condensed part of the array to a new one and go on with this:
A2=A([1:64 81:end]); %copy everything except of this part
Then go on using A2 instead of A in the answer I have given at first.
Kelly Kearney
2014 年 8 月 15 日
A = rand(1,80000);
nuse = 64;
nskip = 16;
new = reshape(A, nuse+nskip, []);
B = new(1:nuse,1) - sum(new(1:nuse,2:end),2);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!