How to segregate data according to its temperature value?
2 views (last 30 days)
Show older comments
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
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 ?
Answers (1)
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)
4 Comments
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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!