Processing large matrix faster

7 ビュー (過去 30 日間)
Manolis Mylonakis
Manolis Mylonakis 2022 年 6 月 19 日
編集済み: Stephen23 2022 年 6 月 19 日
Hello everyone
I need to procces a large matrix (10.104.485x3) called "M". The first two columns are longitude and latitude and the third column is sea depth. I want to trace the coordinates that have depth bigger than 80m. So I try to make a matrix called "coods" with all the corresponding coordinates. Here follows my code:
%M = readmatrix('Bathymetry_Data.txt');
j=1;
for i=1:10104485;
if M(i,3)<-80
coods(j,1)=M(i,1); coods(j,2)=M(i,2);
j=j+1;
end
end
My problem is that the loop never seem to end. I tried pausing after 2 -3 hours and the "i" is still in the very beginning. My operational system has 8 gb Ram. Can anyone tell if I do something wrong, or If I can do anything to make the loop go faster.
Any help will be deeply appreciated, Thank's a lot in advance

採用された回答

Stephen23
Stephen23 2022 年 6 月 19 日
編集済み: Stephen23 2022 年 6 月 19 日
Why are you using a loop? Use logical indexing:
M = readmatrix('Bathymetry_Data.txt');
X = M(:,3)<(-80);
coods = M(X,:);
  1 件のコメント
Manolis Mylonakis
Manolis Mylonakis 2022 年 6 月 19 日
Thank you so much, it works perfect.
Sorry but I as you can tell I am new to matlab

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

その他の回答 (1 件)

Bjorn Gustavsson
Bjorn Gustavsson 2022 年 6 月 19 日
編集済み: Bjorn Gustavsson 2022 年 6 月 19 日
This ought to be done in one go:
coods = M(:,M(:,3)<-80);
In the case you cannot do it in a vectorized way and cannot determine beforehand how big a matrix will be (which makes it impossible to pre-allocate the array), you should try to do the array-growing in blocks instead of element by element, say a couple of 1000 elements at a time and then crop the array down to size at the end.
HTH

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by