Вопрос

Let us assume there is a matrix [mXn]. for example: a=[2 9; 5 7; 8 25; 1 6; 3 9].

I would like to know how to subtract 1st row from 2nd row and so on till end where the difference between two row is 1.

Next subtract first row from 3rd row and so on where the difference is 2.

And after each loop save the output of the new matrix with a name respective to the loop say for difference1 name may be as newMatDif_1 and so one.

diff1----5-2 7-9; 8-2 25-9; .......newMatDiff_1  
diff2----8-2 25-9; 3-8  9-25;.......newMatDiff_2    
diff3----1-2 6-9; .......newMatDiff_3
Это было полезно?

Решение

Your naming scheme is not really feasible or useful in MATLAB, so I've used a 3-dimensional array to hold the differences. The key to this operation is rotating the rows of the array, for which I've defined a function:

rotate_rows = @(A, n) ( [ A((n+1):end,:); A(1:n,:)]);

for r = 1:rows(A)-1
    diffs(:,:,r) = a - rotate_rows(a,r);
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top