Indexing to subtract sections of an array

20 Ansichten (letzte 30 Tage)
Senaasa
Senaasa am 15 Aug. 2014
Bearbeitet: Michael Haderlein am 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

Akzeptierte Antwort

Adam
Adam am 15 Aug. 2014
Bearbeitet: Adam am 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 );

Weitere Antworten (3)

Nitin
Nitin am 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,:));
  1 Kommentar
Senaasa
Senaasa am 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.

Melden Sie sich an, um zu kommentieren.


Michael Haderlein
Michael Haderlein am 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 Kommentare
Senaasa
Senaasa am 15 Aug. 2014
I don't see how this will help me. I have an array of A = 1x80,000, I want to create a new array which contains the first 64 elements of the 80,000 array, then skip the next 16 elements and capture the next 64, skip 16, capture 64, etc. Forget about subt for now. So the new array I'm creating will omit those blocks of 16 elements between the 64 elements. You are reshaping the array but you are using all the elements. This there a way to use reshape to do what I'm trying?
Michael Haderlein
Michael Haderlein am 16 Aug. 2014
Bearbeitet: Michael Haderlein am 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.

Melden Sie sich an, um zu kommentieren.


Kelly Kearney
Kelly Kearney am 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);

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by