Array rows differences and array filling in a loop

2 views (last 30 days)
I have an array like A
A (n x m) =
[a11 a12 a13 ... a1m;
a21 a22 a23 ... a2m;
a31 a32 a33 ... a3m;
a41 a42 a43 ... a4m
....................
an1 an2 an3 ... anm]
I need to count the differences of all elements of the array row by row in all combinations and get an array like B
B (0.5*(n-1)*n x m) =
[a21-a11 a22-a12 a23-a13 ... a2m-a1m;
a31-a11 a32-a12 a33-a13 ... a3m-a1m;
....................................
an1-a11 an2-a12 an3-a13 ... anm-a1m;
a31-a21 a32-a22 a33-a23 ... a3m-a2m;
a41-a21 a42-a22 a43-a23 ... a4m-a2m;
....................................
an1-a(n-1)1 an2-a(n-1)2 ... anm-a(n-1)m]
For example if
A =[1 3;
2 5;
4 6;
7 10;
100 150;
230 270]
that result should be
B = [1 2;
3 3;
6 7;
99 147;
229 267;
2 1;
5 5;
98 145; 228 265; 3 4; 96 144; 226 264; 93 140; 223 260; 130 120]
I tried to start
clc
clear
A = [1 3;2 5;4 6;7 10;100 150;230 270]; % input array example
[n,m] = size(A); % additional variables to set the size of the output array B and the loop limits
B = zeros((n*(n-1)*0.5),m); % empty array for displaying results (n*(n-1)*0.5 - number of rows (differences) in the array)
for i = 1:n
for j = 1:n
B = diff(A(i:j:end,:))
% but I don't understand how to configure the output of the results in a loop to array B after each iteration and with the desired order preserved
end
end

Accepted Answer

Stephen23
Stephen23 on 11 Apr 2023
Edited: Stephen23 on 11 Apr 2023
No loops required, the simple MATLAB approach is to use NCHOOSEK:
A = [1,3;2,5;4,6;7,10;100,150;230,270]
A = 6×2
1 3 2 5 4 6 7 10 100 150 230 270
P = nchoosek(1:size(A,1),2);
B1 = A(P(:,2),:)-A(P(:,1),:)
B1 = 15×2
1 2 3 3 6 7 99 147 229 267 2 1 5 5 98 145 228 265 3 4
And checking against your expected output:
B0 = [1,2;3,3;6,7;99,147;229,267;2,1;5,5;98,145;228,265;3,4;96,144;226,264;93,140;223,260;130,120];
isequal(B0,B1)
ans = logical
1
  2 Comments
Andrey Melnikov
Andrey Melnikov on 12 Apr 2023
Thanks a lot! I knew about this function, but I couldn't understand how it works with arrays which have more than 1 column / row. ) Try to study on your example.

Sign in to comment.

More Answers (2)

Jon
Jon on 11 Apr 2023
Edited: Jon on 11 Apr 2023
This would be one way to do it
A = [1 3;2 5;4 6;7 10;100 150;230 270]; % input array example
[n,m] = size(A); % additional variables to set the size of the output array B and the loop limits
% Make cell array to hold intermediate results
Bcell = cell(n-1,1);
for k = 2:n
Bcell{k-1} = A(k:n,:) - A(k-1,:);
end
% Assign output B matrix
B = cell2mat(Bcell)
B = 15×2
1 2 3 3 6 7 99 147 229 267 2 1 5 5 98 145 228 265 3 4
  3 Comments
Jon
Jon on 11 Apr 2023
You could also define a recursive function to do this
A = [1 3;2 5;4 6;7 10;100 150;230 270]; % input array example
B = alldiff(A)
B = 15×2
1 2 3 3 6 7 99 147 229 267 2 1 5 5 98 145 228 265 3 4
function [B,A] = alldiff(A,B)
% recursive function to compute all row differences
% B = alldiff(A) computes matrix B with all of the row differences
% Check if completed
if size(A,1) == 1
return
end
% Assign B on first entry
if nargin < 2
B = [];
end
B = [B;A(2:end,:)-A(1,:)];
A = A(2:end,:);
% Make recursive call to continue
[B,A] = alldiff(A,B);
end
Jon
Jon on 11 Apr 2023
One minor comment, I think it is more conventional to refer to a matrix as having dimensions m by n, where we consider the rows as the "m's" and the columns as the "n's" but I stuck with your nomenclature.

Sign in to comment.


dpb
dpb on 11 Apr 2023
The cell array @Jon shows is simpler to code and what first strikes, but the answer to the Q? as you posed it to store into the B array directly would look something like (I reformatted the original Q? code to be able to visualize what were actually trying to do more clearly, so that was on me... :) ) --
[nr,nc]=size(A);
B=zeros(nr*(nr-1)/2,nc);
k=0;
for j=1:nr-1 % outer loop -- reference rows
for i=j+1:nr % inner loop -- rows above reference to end
k=k+1; % output row counter
B(k,:)=A(i,:)-A(j,:); % put the differences in that row
end
end

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!