# 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.

1 ビュー (過去 30 日間)
Hossam Amin 2021 年 9 月 18 日
コメント済み: Hossam Amin 2021 年 9 月 18 日
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 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)
##### 1 件のコメント表示非表示 なし
Hossam Amin 2021 年 9 月 18 日
Thanks :)

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by