how to use function "find" over matrices

1 view (last 30 days)
Suppose x = rand(1e5,1e5);
I want to find the lowest number in each column of x without using for loop.
Is it possible?

Accepted Answer

Stephen23
Stephen23 on 10 Apr 2020
"find the first value in each column which lis ess than 0.25 but greater than 0.2."
>> x = rand(13,7)
x =
0.833676 0.654529 0.869031 0.922756 0.586565 0.278136 0.595271
0.335144 0.936490 0.552751 0.676447 0.924786 0.813253 0.388776
0.171351 0.992074 0.426227 0.814150 0.599205 0.378885 0.470132
0.368957 0.162066 0.044178 0.911514 0.431260 0.111011 0.348894
0.787866 0.150796 0.783209 0.406310 0.116503 0.232302 0.350849
0.676606 0.782741 0.251472 0.223849 0.872576 0.665249 0.287961
0.176415 0.750830 0.958001 0.274026 0.107420 0.716966 0.612980
0.030644 0.103396 0.297286 0.256401 0.902245 0.486087 0.812681
0.563573 0.414845 0.615615 0.335131 0.589437 0.396942 0.780523
0.994748 0.314337 0.721215 0.946815 0.446822 0.252527 0.593235
0.438298 0.516228 0.978322 0.183097 0.011558 0.731435 0.948024
0.496606 0.172242 0.224708 0.339960 0.425773 0.730056 0.809002
0.234744 0.195880 0.086287 0.702632 0.708232 0.489843 0.558111
>> y = x>0.2 & x<0.25;
>> [row,col] = find(y & cumsum(y,1)==1)
row =
13
12
6
5
col =
1
3
4
6

More Answers (1)

David Hill
David Hill on 9 Apr 2020
  3 Comments
David Hill
David Hill on 10 Apr 2020
x = rand(1e4);
a = x.*(x>.2&x<.25);
b=arrayfun(@(y)a(find(a(:,y),1),y),1:size(a,2));
parham kianian
parham kianian on 10 Apr 2020
Thank you David. It works well. But the method suggested by Stephen is a little easier.

Sign in to comment.

Categories

Find more on Data Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!