Indexing to subtract sections of an array

16 ビュー (過去 30 日間)
Senaasa
Senaasa 2014 年 8 月 15 日
編集済み: Michael Haderlein 2014 年 8 月 16 日
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

採用された回答

Adam
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 );

その他の回答 (3 件)

Nitin
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,:));
  1 件のコメント
Senaasa
Senaasa 2014 年 8 月 15 日
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
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 件のコメント
Senaasa
Senaasa 2014 年 8 月 15 日
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 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
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);

カテゴリ

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