I have a matrix. I can't figure out how to subtract row 1 from row 5, row 2 from row 6, row 3 from row 7, row 4 from row 8. My code isn't working.

2 次查看(过去 30 天)
A = [2.5 3.5 7.5 8.5 ;... %1
9.5 5.5 6.5 1.5;... %2
2.5 3.5 7.5 8.5; ... %3
9.5 5.5 6.5 1.5;... %4
2.5 3.5 7.5 8.5; ... %5
9.5 5.5 6.5 1.5;... %6
2.5 3.5 7.5 8.5;... %7
9.5 5.5 6.5 1.5]; %8
for i=1:size(A,1)
for j=1:size(A,1)/2
tmp(i,:) = A(i,:)-A(j,:)
end
% tmp(i,:) = A(i,:)-A(4+j,:)
end
My code is not working very well. I need a loop that can work for any matrix size.

采纳的回答

TADA
TADA 2021-9-18
if the functionality is to subtract the row in index n+4 from the row in index n
A = [2.5 3.5 7.5 8.5 ;... %1
9.5 5.5 6.5 1.5;... %2
2.5 3.5 7.5 8.5; ... %3
9.5 5.5 6.5 1.5;... %4
2.5 3.5 7.5 8.5; ... %5
9.5 5.5 6.5 1.5;... %6
2.5 3.5 7.5 8.5;... %7
9.5 5.5 6.5 1.5]; %8
subtractInterval = 4;
tmp = A(1:end-subtractInterval, :) - A(subtractInterval+1:end, :)
size(tmp)
A = magic(10);
tmp = A(1:end-subtractInterval, :) - A(subtractInterval+1:end, :)
size(tmp)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by