# Indexing to subtract sections of an array

36 views (last 30 days)
Senaasa on 15 Aug 2014
Edited: Michael Haderlein on 16 Aug 2014
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

Edited: Adam on 15 Aug 2014
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 );

Nitin on 15 Aug 2014
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,:));
Senaasa on 15 Aug 2014
Thanks for your reply, see my comment to Michael's post, I don't think your method would work because reshape requires you have rows that have the same number of elements.

Michael Haderlein on 15 Aug 2014
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 CommentsShowHide 1 older comment
Michael Haderlein on 16 Aug 2014
Edited: Michael Haderlein on 16 Aug 2014
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 on 15 Aug 2014
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);