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 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

TADA
TADA am 18 Sep. 2021
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)

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by