# How to select specific values and corresponding cell positions from 31 x 12 matrix?

11 views (last 30 days)
Parthu P on 6 Nov 2019
Commented: Fabio Freschi on 7 Nov 2019
Hi,
I have 31 x 12 matrix (A) with daily temperature for one year. Values range between 10.2 and 39°C.
How to find a new metrix B (? x 12) with all values (in each column) less than 1.15 times minimum value (ie. < 10.2*1.15) and corresponding cell positions?
Parthu P on 6 Nov 2019
Hi,
I have 31 x 12 matrix (A) with daily temperature for one year. Values range between 10.2 and 39°C.
How to find a new metrix B (? x 12) with all values (in each column) less than 1.15 times minimum value (ie. < 10.2*1.15) and corresponding cell positions?

Fabio Freschi on 6 Nov 2019
Edited: Fabio Freschi on 6 Nov 2019
Because the number of rows in B is not fixed, you can store the result of your check in a cell array
% create the matrix
A = (39-10.2)*rand(31,12)+10.2;
% tolerance
tol = 1.15;
% get minimum
minA = min(A(:));
% cell array with required entries
B = arrayfun(@(i)A(A(:,i) < tol*minA,i),1:size(A,2),'UniformOutput',false);
You can now access your data using B{i}, where i is the index of the month
You can also get the pointers to the desired data using find
[iDay,jMonth] = find(A < tol*minA);
Fabio Freschi on 7 Nov 2019
B has only one row, but each cell contains all values oq your query:
>> B
B =
1×12 cell array
Columns 1 through 6
{5×1 double} {2×1 double} {0×1 double} {2×1 double} {[10.4252]} {0×1 double}
Columns 7 through 12
{2×1 double} {[10.7914]} {2×1 double} {4×1 double} {[11.4044]} {2×1 double}
as you can see, for example, B{1} has 5 entries.
How do you expect to have B if the different montsh can have a different number of days with temperature below your threshold? In other words, what is the size of the expexted output?
Another thing you can do is to have a 31 x 12 B matrix, where all values above the threshold are set to zero or NaN. In this case you can use
B = A;
B(A >= tol*minA) = 0;