Function not processing dat for each element in array

2 views (last 30 days)
Dharmesh Joshi on 27 Jun 2022
Edited: Jan on 30 Jun 2022
Hi
I need to preform a simple temperture compensation.
I have variable called iot_temperture, which returns the temperture for 10K data points.
I then have a function
function per = tempcorrect(n)
%disp(n)
if n >= 50
per = 115;
elseif n >=40
per = 108;
elseif n >=30
per = 145;
elseif n >=20
per = 120;
% elseif temp >=10
% per = 95;
% elseif temp >=0
% per = 87;
% elseif temp >=-10
% per = 85;
% elseif temp >=-20
% per = 90;
% elseif temp >=-30
% per = 105;
else
per = 100;
end
% x=[-30,-20,-10,0,10,20,30,40,50]
% 105 90 85 87 95 100 105 108 115
end
The function aims to return the the correction factor for a perticular
Therefore when i run the following code, i am expecting correction factor for all 10K points, but all i get is 100 ouput.
a=tempcorrect(iot_temperture)
What am i doing wrong?

Voss on 27 Jun 2022
The n given to tempcorrect is a vector (of length 10000). When you use a vector (or matrix, or any array) in an if statement, the if condition is considered true only if it is true for all elements of that vector (or matrix or array).
if [1 2 3] > 2
disp('true');
else
disp('false')
end
false
if [1 2 3] > 0
disp('true');
else
disp('false')
end
true
It turns out the n given to tempcorrect is such that all the if and elseif conditions checked in the function are false for at least one element of n, so the else block executes and you get per = 100 as output.
To have tempcorrect operate on one element of n at a time, you can give it one element of n at a time and collect the results in a vector:
a = zeros(size(iot_temperture))
for ii = 1:numel(iot_temperture)
a(ii) = tempcorrect(iot_temperture(ii));
end
Or you can adapt tempcorrect to work for array inputs, either by putting the loop in the function itself, or by using logical indexing:
function per = tempcorrect(n)
per = 100*ones(size(n)); % initial value 100
per(n >= 50) = 115;
per(n < 50 & n >= 40) = 108;
per(n < 40 & n >= 30) = 145;
per(n < 30 & n >= 20) = 120;
% not necessary if using 100 as initial value:
% per(n < 20) = 100;
% per(n < 20 & n >= 10) = 95;
% per(n < 10 & n >= 0) = 87;
% per(n < 0 & n >= -10) = 85;
% per(n < -10 & n >= -20) = 90;
% per(n < -20 & n >= -30) = 105;
% not necessary if using 100 as initial value:
% per(n < -30) = 100;
end
Voss on 29 Jun 2022
You're welcome!

Jan on 27 Jun 2022
Edited: Jan on 30 Jun 2022
Remember, that Matlab's IF condition must be a scalar. If you provide n as a vector, the line:
if n >= 50
implicitly performs:
if all(n >= 50) && ~isempty(n)
You either need a loop over the elements of n or logical indexing:
per = zeros(size(n));
for k = 1:numel(n)
if n(k) >= 50
per(k) = 115;
...
or
per = repmat(100, size(n));
per(n >= 20) = 120; % From small to large values
per(n >= 30) = 145;
or
Edge = [-inf, 20, 30, 40, 50, inf];
Value = [100, 120, 145, 108, 115];
Group = discretize(n, Edge);
per = Value(Group)

Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by