manhonest69
manhonest69

Reputation: 3

How to find non zero minimum array in a 2D matrix in matlab

I have a 2D matrix as shown below:

A = 

1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.8193    0.6429    0.4731
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.8172    0.6395    0.4696
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.8099    0.6285    0.4582
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.7938    0.6066    0.4364
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.7588    0.5675    0.4006
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.8527    0.6739    0.5039    0.3477
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.8825    0.7371    0.5803    0.4264    0.2807
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.7928    0.6327    0.4838    0.3406    0.2054
1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    0.8846    0.7945    0.6560    0.5173    0.3816    0.2469    0.1131
1.0000    1.0000    1.0000    1.0000    1.0000    0.8598    0.7438    0.6373    0.5194    0.3988    0.2785    0.1522         0
1.0000    1.0000    1.0000    0.8710    0.7956    0.6954    0.5935    0.4916    0.3856    0.2799    0.1814    0.0834         0
0.7915    0.7845    0.7581    0.6884    0.6159    0.5326    0.4432    0.3500    0.2514    0.1538    0.0838         0         0
0.5972    0.5882    0.5596    0.5085    0.4471    0.3760    0.2967    0.2137    0.1163         0         0         0         0
0.4208    0.4116    0.3836    0.3389    0.2881    0.2274    0.1540    0.0919         0         0         0         0         0
0.2629    0.2536    0.2244    0.1755    0.1388    0.0915         0         0         0         0         0         0         0
0.1235    0.1155    0.0850         0         0         0         0         0         0         0         0         0         0
     0         0         0         0         0         0         0         0         0         0         0         0         0

I want to remove its zero arrays and then find the minimum value of each column and put those minimum values in a vector.

I just used find(A ~= 0) but it's not working.

How can I do that?

Upvotes: 0

Views: 268

Answers (1)

rayryeng
rayryeng

Reputation: 104484

Replace all of the zeroes with NaN, then apply min to each column independently. Setting values to NaN in a matrix / vector allows min to ignore those values, so you'd effectively be ignoring the zeroes. Assuming that your matrix is in A, do this:

Anan = A;
Anan(A == 0) = NaN;
out = min(Anan, [], 1);

Upvotes: 2

Related Questions