Combinations of array rows with exclusion
3 ビュー (過去 30 日間)
古いコメントを表示
I've a question that maybe has an "easy" solution. I've searched (and I still am) but haven't found yet. Can anyone could help me or point out a solution for this?
I've a result of fixed pairs (latitude longitude) for a n points / rows (usually 8 or 10 max number of points/pair of rows), now i need to combine this fixed pairs from n-1,n-2,...n-k, until n-k = 4 (minimum number of points/pair of rows). For instance, If have an array of 6x2:
[20 10;20 15;20 30;22 15;25 10; 30 10], i would need to have a first combination set of 5 rows leaving one out, then select another 5 set of rows, leaving other row out and including the first one, until every 5 set of possible row selections have been made and stored in another array with all combinations. Then, from the initail 6 row array set, I would need to select 4 rows of pairs, in the same manner i did for the selection and combination of 5 rows, until every possible 4 rows pairs of 6 have been combined.
Each row pair / point is fixed. Lat long of point 1 must always be the same lat long pair.
Maybe this is hard to do, but if anyone has some kind of solution for this it would be very helpfull
Thank you so much,
Pedro
2 件のコメント
Jorg Woehl
2021 年 3 月 8 日
Hi Pedro, if I understand you correctly you want to create all possible subarrays of your initial n-by-2 array, creating n (n-1)-by-2 arrays by leaving one row out. For example, [row1; row2; row3; row4; row5; row6] becomes [row2; row3; row4; row5; row6] or [row1; row3; row4; row5; row6] or ..., and so on.
You then said these subarrays are to be "stored in another array with all previous combinations" - how exactly? Do you want them all to be concatenated into one 30-by-2 array, one after another? Or stored in a 5-by-2-by-6 array?
採用された回答
Jorg Woehl
2021 年 3 月 8 日
編集済み: Jorg Woehl
2021 年 3 月 8 日
This answer does not have any of the duplicates that are present in my previous answer:
A = [20 10;20 15;20 30;22 15;25 10; 30 10];
k = size(A,1)-4; % max. number of rows to delete
r = [];
for i = 1:k
r = rowsToDelete(size(A,1), r);
for j = 1:size(r,2)
subA = A;
subA(r{j},:) = []; % delete the rows stored in r{j}
cellArr{i}(:,:,j) = subA ; % ... and store the result in B
end
end
function newr = rowsToDelete(n,r)
if isempty(r)
% set rows-to-delete to 1, 2, ..., n
for i = 1:n
newr{i} = i;
end
else
idx = 1;
for i = 1:size(r,2)
% add another row to delete to existing ones
for j = r{i}(end)+1:n
newr{idx} = [r{i}, j];
idx = idx+1;
end
end
end
end
2 件のコメント
Jorg Woehl
2021 年 3 月 9 日
編集済み: Jorg Woehl
2021 年 3 月 10 日
Hi Pedro,
Thank you for your kind words. It is good to hear that you got everything working, even if the result is not what you had hoped for. But that's what research is all about (I am a research scientist myself).
I have received an email where you asked about how to access the combinations in the cell array, which I wanted to briefly answer for the benefit of other users (even though you have found the solution):
cellArr{1} contains six 5-by-2 matrices that can be accessed with cellArr{1}(:,:,1) through cellArr{1}(:,:,6). Same for the fifteen 4-by-2 matrices contained in cellArr{2}, you can access them at cellArr{2}(:,:,i), where i is an index from 1 to 15 or size(cellArr{2},3), and so on.
その他の回答 (1 件)
Jorg Woehl
2021 年 3 月 8 日
OK, so how about this?
A = [20 10;20 15;20 30;22 15;25 10; 30 10];
cellArr{1} = deleteOneRow(A);
for i = 2:size(A,1)-4
cellArr{i} = deleteOneRow(cellArr{i-1});
end
function B = deleteOneRow(A)
rows = size(A,1);
cols = size(A,2);
matrices = size(A,3);
B = NaN([rows-1, cols, rows*matrices]);
for n=1:matrices
for row=1:rows
subA = A(:,:,n); % read one matrix from the array
subA(row,:) = []; % delete one row from it
B(:,:,(n-1)*size(A,1)+row) = subA; % ... and store it in B
end
end
end
The output is contained in the cell array cellArr. cellArr{1} gives you all possible (n-1)-by-2 matrices, cellArr{2} all possible (n-2)-by-2 matrices, and so on. This also works with A being larger than 6-by-2, such as 7-by-2, 8-by-2 etc.
4 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!