フィルターのクリア

What is the most efficient way to find the position in the column of a matrix where the value drops below a given threshold (values are constantly decreasing down the columns)?

1 回表示 (過去 30 日間)
I have a large (up to 1000x1000) matrix which is the solution to a pde - the columns are the increments in time and the rows are the increments in space. The values down each column are decreasing and I want to find the row of each column where the value drops below a certain value (1 in the code below) and store these values in a vector where the value at each position is the row where it drops below the threshold. My method works perfectly well but is very slow, is there a better way?
My code:
timeivector = 2:state.Numberoftimesteps; %starts at 2 since initial condition is zero everywhere
spaceivector = 1:state.Numberofspacesteps;
for ti = timeivector
for xi = spaceivector
if largematrix(xi,ti) <= 1
continue
end
outputvector(ti) = xi+1;
end
end

採用された回答

Joel Miller
Joel Miller 2017 年 12 月 13 日
Similar to the previous answer, but without the meshgrid:
largelogical = largematrix <= 1;
[value, index] = max(largelogical);
  2 件のコメント
Rik
Rik 2017 年 12 月 13 日
value will by definition be 1, but I must admit this is more elegant (and faster) than my solution.
J2A2B2
J2A2B2 2017 年 12 月 14 日
perfect, it takes practically no time at all to run. thanks

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

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2017 年 12 月 13 日
first_row_below_threshold = sum(largematrix >= 1, 1) + 1;
Note: if none of the rows are below the threshold then the value will be 1 more than the number of rows in the matrix.
  1 件のコメント
Jos (10584)
Jos (10584) 2017 年 12 月 14 日
This makes excellent use of the given fact that the columns are in decreasing order. +1

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


Birdman
Birdman 2017 年 12 月 13 日
編集済み: Birdman 2017 年 12 月 13 日
One approach:
outputvector=largematrix(largematrix<=1);
To find specific rows and columns for the values:
[r,c]=find(largematrix<=1);

Rik
Rik 2017 年 12 月 13 日
編集済み: Rik 2017 年 12 月 13 日
It will be much faster to use find, but you don't even need to.
You can use meshgrid to generate indices (like repmat((1:1000)',1,1000), but faster and clearer). Then use logical indexing to set all positions in the grid to inf for values>1, then using min will give you a vector with indices.
PS this will only find you 1 index per column

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by