Array rows differences and array filling in a loop

2 ビュー (過去 30 日間)
Andrey Melnikov
Andrey Melnikov 2023 年 4 月 11 日
コメント済み: Andrey Melnikov 2023 年 4 月 12 日
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

採用された回答

Stephen23
Stephen23 2023 年 4 月 11 日
編集済み: Stephen23 2023 年 4 月 11 日
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 件のコメント
Jon
Jon 2023 年 4 月 11 日
Wow, that is truly an elegant solution!
Andrey Melnikov
Andrey Melnikov 2023 年 4 月 12 日
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.

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

その他の回答 (2 件)

Jon
Jon 2023 年 4 月 11 日
編集済み: Jon 2023 年 4 月 11 日
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 件のコメント
Jon
Jon 2023 年 4 月 11 日
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 2023 年 4 月 11 日
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.

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


dpb
dpb 2023 年 4 月 11 日
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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by