簡體   English   中英

根據X,Y或Z值對點雲的坐標進行排序

[英]Sorting coordinates of point cloud in accordance with X, Y or Z value

A是3D(X,Y,Z)中的一系列點坐標,例如:

>> A = [1 2 0;3 4 7;5 6 9;9 0 5;7 8 4]

A = 1 2 0 3 4 7 5 6 9 9 0 5 7 8 4

我想根據"Y" (second column)值對矩陣進行排序。

這是我正在使用的代碼:

>> tic;[~, loc] = sort(A(:,2)); SortedA = A(loc,:) toc;

SortedA = 9 0 5 1 2 0 3 4 7 5 6 9 7 8 4

Elapsed time is **0.001525** seconds.

但是,對於大量數據,它可能非常慢。 如果有人知道更有效的方法,我將不勝感激。

MATLAB確實有一個名為sortrows()的功能,但根據我的經驗,它往往與你為一般非結構化矩陣所做的一樣慢。

測試:

N = 1e4;
A = rand(N,N);
tic;[~, loc] = sort(A(:,2));
SortedA = A(loc,:);
toc;

tic; sortrows(A,2); toc;

得到:

Elapsed time is 0.515903 seconds.
Elapsed time is 0.525725 seconds.

介紹性討論

這個答案將主要討論如何利用計算有效的GPU來解決所述問題。 該問題中提出的問題的解決方案代碼是 -

[~, loc] = sort(A(:,2));
SortedA = A(loc,:);

基本上有兩個部分 -

  1. 選擇第二列,對它們進行排序並獲取已排序的索引。
  2. 使用已排序的索引索引輸入矩陣的行。

現在, Part 1是計算密集型的,可以移植到GPU ,但Part 2是索引工作,可以在CPU本身上完成。

提出的解決方案

因此,考慮到所有這些,高效的GPU解決方案將是 -

gA = gpuArray(A(:,2)); %// Port only the second column of input matrix to GPU
[~, gloc] = sort(gA); %// compute sorted indices on GPU
SortedA = A(gather(gloc),:); %// get the sorted indices back to CPU with `gather` 
                             %// and then use them to get sorted A

標桿

下面介紹的是將GPU版本與原始解決方案進行比較的基准代碼,但請記住,由於我們在不同硬件上運行GPU代碼與最初在CPU上運行的解決方案相比,基准測試結果可能會有所不同系統到系統。

這是基准代碼 -

N = 3000000; %// datasize (number of rows in input)
A = rand(N,3); %// generate random large input

disp('------------------ With original solution on CPU')
tic
[~, loc] = sort(A(:,2));
SortedA = A(loc,:);
toc, clear SortedA loc

disp('------------------ With proposed solution on GPU')
tic
gA = gpuArray(A(:,2));
[~, gloc] = sort(gA);
SortedA = A(gather(gloc),:);
toc

以下是基准測試結果 -

------------------ With original solution on CPU
Elapsed time is 0.795616 seconds.
------------------ With proposed solution on GPU
Elapsed time is 0.465643 seconds.

因此,如果你有足夠的GPU ,那么現在是時候嘗試使用GPU來排序相關的問題了,而MATLAB提供了這樣簡單的GPU移植解決方案。


系統配置

MATLAB Version: 8.3.0.532 (R2014a)
Operating System: Windows 7
RAM: 3GB
CPU Model: Intel® Pentium® Processor E5400 (2M Cache, 2.70 GHz)
GPU Model: GTX 750Ti 2GB

嘗試sortrows ,指定第2列:

Asorted = sortrows(A,2)

現在我測試它更簡單但實際上更慢...如果您只考慮1列進行排序,顯然sortrows並不是那么好。 當您按特定順序考慮多個列時,這可能是最好的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM