1

I need help with the following code:

N = size(data1,1)/2;
c=NaN((size(data1,1)/2),size(data1,2));
kk=0;
for ii=1:2:((size(data1,1)/2)-1)
c(kk+(1:N-ii),:)=bsxfun(@minus,data1(ii,1:end),data1(ii+1,1:end))
kk=kk+N-ii;
end

The code is attempting to take the difference between all column values between two rows, then get the difference between the next two different rows and so on. Example:

matrix a
1  2  3  4
4  5  6  7
8  9  10 11
12 13 14 15
16 17 18 19
20 21 22 23


matrix b
3 3 3 3
4 4 4 4
4 4 4 4
4

1 回答 1

1

怎么样:

a(2:2:end,:) - a(1:2:end-1,:)

编辑:根据您的 for 版本:

data1 = [1  2  3  4; 4  5  6  7; 8  9  10 11; 12 13 14 15; 16 17 18 19; 20 21 22 23];
[row,col] = size(data1);
row = row/2;
c = nan(row,col);
for ii = 1:row
    c(ii,:) = bsxfun(@minus,data1(2*ii,:),data1(2*ii-1,:));
end
于 2012-08-04T03:40:15.120 回答