简体   繁体   中英

Slice Matrix matlab no loop

i have the following code, where d is a matrix with dimension 94x290:

samples=268;
dim_cov=23;
for q=1:samples
    D(:,q)=(sum(d(:,q+(1:dim_cov)-1),2));
end

Is there a way to do this without a loop?

Does this work?

dim_cov = 23;
cs = [zeros(size(d,1), 1) cumsum(d, 2)];
result = cs(:,1+dim_cov:end) - cs(:,1:end-dim_cov);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM