selecting i element non overlapping subsets of an array of length n
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
SSG_newbiecoder
am 22 Dez. 2017
Beantwortet: Jos (10584)
am 22 Dez. 2017
Hello, I have an array of length n. I need to select the first i elements and manipulate it and then take the next non overlapping i elements and apply the same manipulations. how can I do this selection in matlab ?
0 Kommentare
Akzeptierte Antwort
Jos (10584)
am 22 Dez. 2017
or you can use a simple for-loop
A = 1:10
n = 3 ;
N = numel(A) ;
for k=1:n:N
ix = k:min(k+n-1,N) ;
subA = A(ix) ;
disp(subA) ; % process sub array
end
If you have a function to process the data you can also use arrayfun, for instance, like this:
Cout = arrayfun(@(k) myfun(A(k:min(k+n-1,numel(A)))), 1:n:numel(A), 'un',0) ;
% return the outputs of myFun into distinct cells of Cout
0 Kommentare
Weitere Antworten (1)
Walter Roberson
am 22 Dez. 2017
If the array is certain to be a multiple of the number of elements you are taking at a time, then use reshape() to convert that size into columns and then you can process by columns.
If the array might not be an exact multiple but you have the Signal Processing Toolbox, then use buffer() to convert it into columns.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!