How to segregate data according to its temperature value?

2 views (last 30 days)
Hi all.
Suppose I have two 3d arrays, A and B, and both of them are of the same size (10000x100x100).
A is temperature values and B is water levels.
Values in A is associated with values in B of the same index. For example, the corresponding value for A(1,1,1) is B(1,1,1), the same goes for A(1,1,2) and B(1,1,2) and so on.
My aim is to segregate all the values in B into their respective temperature bins base on their temperature values in A.
All temperature bins are of one °C width. (E.g. any temperature value between 14 and 15 °C belongs to one group, and any temperature value between 15 and 16 °C belongs to another group.)
May I know how to do the above in an efficient manner? If all the above is too confusing, you can take a look at my code below. However, I feel that my code is not very efficient as there are a lot of 1d arrays and I am looking for ways to improve it. Thanks in advance.
My code:
max(A,[],'all'); %To find out the largest temperature bin value
min(nonzeros(A,[],'all'); %To find out the smallest temperature bin value
Map=cat(4,A,B);
size(Map);
%All the following variables are temperature bins
B_temp_21=[];
B_temp_22=[];
B_temp_23=[];
B_temp_24=[];
B_temp_25=[];
B_temp_26=[];
B_temp_27=[];
B_temp_28=[];
B_temp_29=[];
B_temp_30=[];
B_temp_31=[];
for i = 1:10000
for j = 1:100
for k = 1:100
if Map(i,j,k,1)>0
if fix(Map(i,j,k,1)) ==21
B_temp_21(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 22
B_temp_22(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 23
B_temp_23(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 24
B_temp_24(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 25
B_temp_25(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 26
B_temp_26(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 27
B_temp_27(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 28
B_temp_28(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 29
B_temp_29(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 30
B_temp_30(end+1)=Map(i,j,k,2);
elseif fix(Map(i,j,k,1)) == 31
B_temp_31(end+1)=Map(i,j,k,2);
else
continue
end
else
continue
end
end
end
i %Keep track of progress
end
  2 Comments
Torsten
Torsten on 14 Sep 2022
Edited: Torsten on 14 Sep 2022
My aim is to segregate all the values in B into their respective temperature bins base on their temperature values in A.
What exactly do you mean by this ? For each temperature interval, you want to get the vector of water levels that belong to this temperature interval ?
Baiwen Ding
Baiwen Ding on 14 Sep 2022
Yes this is what i meant sorry if it was confusing as i couldnt find any better words for it

Sign in to comment.

Answers (1)

Torsten
Torsten on 14 Sep 2022
Edited: Torsten on 14 Sep 2022
Use MATLAB's "discretize" for the temperature array and use the so received vector Y of bins to split the vector of water levels analogously:
Example:
rng('default')
A = rand(10,23,5);
B = rand(10,23,5);
X = linspace(0,1,10);
Y = discretize(A(:),X);
Bvec = B(:);
C = arrayfun(@(i)Bvec(Y==i),1:numel(X),'UniformOutput',0)
C = 1×10 cell array
{132×1 double} {139×1 double} {121×1 double} {131×1 double} {127×1 double} {130×1 double} {140×1 double} {110×1 double} {120×1 double} {0×1 double}
  4 Comments
Baiwen Ding
Baiwen Ding on 14 Sep 2022
Hi i have tried the discretize function previously. However the code is still very iterative and long just like my current code. I am looking forwards to short my current code if possible. Thanks
Torsten
Torsten on 14 Sep 2022
Edited: Torsten on 14 Sep 2022
"discretize" is fast as fast can be.
So your complete loop and if constructions can be replaced by one call to "discretize", I guess.
Translated to your code:
X = floor(min(A(:))):ceil(max(A(:)));
Y = discretize(A(:),X);
Bvec = B(:);
B_temp = arrayfun(@(i)Bvec(Y==i),1:numel(X),'UniformOutput',0)
should substitute you complete code from above.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!