unexpected results of a function in matlab - matlab

Normally this function should give me the values ​​1, 2, 3 or 4. but when I use it, I get 0, 1 or 2. Could you help me to know where is the problem:
function Vecteur_retour = var_Test(Test)
AA = Test;
var_Test = zeros(1,2000);
for i=3:1:2000
if AA(i)<=AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=1;
else
if AA(i)<=AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=2;
if AA(i)>AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=3;
else
if AA(i)>AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=4;
end
end
end
end
end
Vecteur_retour = var_Test;

Vector comparisons will be much faster:
var_Test = ones(1,2000);
delta_Test = diff(Test);
var_Test([0 0 delta_Test(1:end-1)] > 0) = 2;
var_Test([0 delta_Test] > 0) = var_Test([0 delta_Test] > 0) + 2;
var_Test(1:2) = 0;

Probably because you never reach the cases var_Test(i) = 3 or var_Test(i) = 4.
You have a problem with your if and end blocks. The way you have it, case 3 is only reached if case 2 is hit first, but these are contradictory.
You want code more like.
function Vecteur_retour = var_Test(Test)
AA = Test;
var_Test = zeros(1,2000);
for i=3:1:2000
if AA(i)<=AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=1;
else
if AA(i)<=AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=2;
else % you forgot this else
if AA(i)>AA(i-1) && AA(i-1)<=AA(i-2)
var_Test(i)=3;
else
if AA(i)>AA(i-1) && AA(i-1)>AA(i-2)
var_Test(i)=4;
end
end
end
end
end
Vecteur_retour = var_Test;
Careful indentation would have helped here.

Related

Test for scalar function inputs is failing

Despite using isscalar, isinteger... the result after running is still true, I still had this test which is incorrect and in relation with Scalar values.
My program:
function valid = valid_date(year, month, day)
[y1 y2] = size(year);
[m1 m2] = size(month);
[d1 d2] = size(day);
if (mod(year,4)==0 && mod(year,100)~=0) || mod(year,400)==0
is_leap_year = 1;
else
is_leap_year = 0;
end
if ~(y1==y2==d1==d2==m1==m2==1)
valid = false;
elseif any(month == [1, 3, 5, 7, 8, 10, 12])
valid = (day >0 && day <= 31)
elseif any(month == [4, 6, 9, 11])
valid = day >0 && day <= 30;
elseif month == 2 && is_leap_year == 1
valid = (day >0 && day <=29);
elseif month == 2 && is_leap_year == 0
valid = (day >0 && day <=28);
else
valid = false;
end
end
The result after submitting my program, all tests are passed except the one related to scalar values:
Why did my program fail on the non-scalar test?
The way you're checking for scalars is really not well defined.
~(y1==y2==d1==d2==m1==m2==1)
This chained equivalence check is not the same as checking if all of your variables are equal to 1, consider the following counter-example:
1==0==0==1 % Output: 1
In this case none of your comparison variables should be 0, so you might skirt this issue, but it's still best to avoid it. You're also contending with floating point comparisons which are prone to issues.
You should use isscalar, you say you tried it but didn't show how. Something like this should work:
function valid = valid_date(year, month, day)
if ~( isscalar(year) && isscalar(month) && isscalar(day) )
valid = false;
return
end
% ... other code now we know inputs are scalar
end
Thank you so much #Wolfie. The problem is solved.
I used what you told me about and put it at the beginning of my function.
I show you guys the code, in case you had the same error:
function valid = valid_date(year, month, day)
if ~(isscalar(year) && isscalar(month) && isscalar(day))
valid = false;
return
end
if ((mod(year,4)==0 && mod(year,100)~=0) || mod(year,400)==0)
is_leap_year = 1;
else
is_leap_year = 0;
end
if any(month == [1, 3, 5, 7, 8, 10, 12])
valid = (day >0 && day <= 31);
elseif any(month == [4, 6, 9, 11])
valid = day >0 && day <= 30;
elseif (month == 2 && is_leap_year == 1)
valid = (day >0 && day <=29);
elseif (month == 2 && is_leap_year == 0)
valid = (day >0 && day <=28);
else
valid = false;
end
end
Yaaay all the tests are passed lhamdullah!

How to print ONCE if there are multiple correct answers? (MATLAB)

So I have arr = randi([0,20],20,1). I want to show: If there are numbers less than 5, fprintf('Yes\n') only once. Im using a for loop (for i = 1 : length(arr)) and indexing it.
As your description, maybe you need if statement within for loop like below
for i = 1:length(arr)
if arr(i) < 5
fprintf('Yes\n');
break
end
end
If you want to print Yes once, you can try
if any(arr < 5)
fprintf('Yes\n')
endif
If you don't want to use break, the code below might be an option
for i = 1:min(find(arr <5))
if (arr(i) < 5)
fprintf('Yes\n');
end
end
You can use a break statement upon finding the first value under 5 and printing the Yes statement.
Using a break Statement:
arr = randi([0,20],20,1);
for i = 1: length(arr)
if arr(i) < 5
fprintf("Yes\n");
break;
end
end
Extension:
By Using any() Function:
Alternatively, if you'd like to concise it down without the need for a for-loop the any() function can be used to determine if any values within the array meet a condition in this case arr < 5.
arr = randi([0,20],20,1);
if(any(arr < 5))
fprintf("Yes\n");
end
By Using a While Loop:
Check = 0;
arr = randi([0,20],20,1);
i = 1;
while (Check == 0 && i < length(arr))
if arr(i) < 5
fprintf("Yes\n");
Check = 1;
end
i = i + 1;
end

MATLAB - reading multiple dat files for loop

Hello everyone I'm trying to make a loop file to read several files, this is what I have done so far:
anoini = 1980;
anofin = 1981;
mesini = 1;
mesfin = 1;
diai=1;
diaf=1;
nano = (anofin-anoini)+1;
if (mesini == 1) || (mesini == 3) || (mesini == 5) || (mesini == 7) || (mesini == 8) || (mesini == 10) || (mesini == 12)
lmes = 31;
elseif (mesini == 4) || (mesini == 6) || (mesini == 9) || (mesini == 11)
lmes = 30;
elseif (mesini == 2)
lmes = 28;
end
for idia=1:lmes
for iano = anoini:anofin
for nn = 1:nano
D_1{nn,idia} = load(sprintf('F:\\salidas_nam\\%d\\%d%0.2u%0.2u06_NAM_day01.dat',iano,iano,mesini,idia));
end
end
end
For example I want this two files, but it only seems to read one
"F:\salidas_nam\1980\1980010106_NAM_day01.dat"
"F:\salidas_nam\1981\1981010106_NAM_day01.dat"
The idea is to automatize this so I can pick which years to read, it is not necesary to use sprintf, if someone knows how to do this in another way I will apreciate it.
I think the problem is in your inner loop. You were iterating over nn but not using it in the construction of the filename. So you ended up loading each file twice:
Therefore your output looked like this (repeated files):
D_1{1,1} = F:\salidas_nam\1980\1980010106_NAM_day01.dat
D_1{2,1} = F:\salidas_nam\1980\1980010106_NAM_day01.dat
D_1{1,2} = F:\salidas_nam\1981\1981010106_NAM_day01.dat
D_1{2,2} = F:\salidas_nam\1981\1981010106_NAM_day01.dat
...
However, I think you want this (no repeats):
D_1{1,1} = F:\salidas_nam\1980\1980010106_NAM_day01.dat
D_1{2,1} = F:\salidas_nam\1981\1981010106_NAM_day01.dat
D_1{1,2} = F:\salidas_nam\1980\1980010206_NAM_day01.dat
D_1{2,2} = F:\salidas_nam\1981\1981010206_NAM_day01.dat
...
If that is the case then you can drop the inner for loop and do something like this:
for idia=1:lmes
nn = 1;
for iano = anoini:anofin
D_1{nn,idia} = load(sprintf('F:\\salidas_nam\\%d\\%d%0.2u%0.2u06_NAM_day01.dat',iano,iano,mesini,idia));
nn = nn+1;
end
end
This does not address the potential leapyear issues you might have as Adriaan mentioned in the comments.

Creating a for-loop that stores values in a new variable

I'm quite new to Matlab so excuse me for the basic question.
I need to make a for-loop that repeats it's self 384 times.
So :
for i=1:384
I now need the for loop to check if 2 certain variables have the value 1 through 10, and then let them store this in a new variable with that value.
So:
if x==1
somevariable = 1
elseif x== 2
saomevariable = 2
..
..
..
elseif y = 1
someothervariable = 1
etc etc.
Is there a way to write this more efficient?
Thank you!
The first think you can do is:
if(x >= 1 && x <= 10)
somevariable=x;
end
if(y >= 1 && y <= 10)
someohtervariable=y;
end
If you could post more information about "x" and "y", perhaps your script can be further "improved".
Hope this helps.

Improving runtime in Matlab?

I have some code that is taking a long time to run(several hours) and I think it is because it is doing a lot of comparisons in the if statement. I would like it to run faster, does anyone have any helpful suggestions to improve the runtime? If anyone has a different idea of what is slowing the code down so I could try and fix that it would be appreciated.
xPI = zeros(1,1783);
argList2 = zeros(1,1783);
aspList2 = zeros(1,1783);
cysList2 = zeros(1,1783);
gluList2 = zeros(1,1783);
hisList2 = zeros(1,1783);
lysList2 = zeros(1,1783);
tyrList2 = zeros(1,1783);
minList= xlsread('20110627.xls','CM19:CM25');
maxList= xlsread('20110627.xls','CN19:CN25');
N = length(pIList);
for i = 1:N
if (argList(i)>= minList(1) && argList(i) <= maxList(1)) ...
&& (aspList(i)>= minList(2) && aspList(i) <= maxList(2)) ...
&& (cysList(i)>= minList(3) && cysList(i) <= maxList(3)) ...
&& (gluList(i)>= minList(4) && gluList(i) <= maxList(4)) ...
&& (hisList(i)>= minList(5) && hisList(i) <= maxList(5)) ...
&& (lysList(i)>= minList(6) && lysList(i) <= maxList(6)) ...
&& (tyrList(i)>= minList(7) && tyrList(i) <= maxList(7))
xPI(i) = pIList(i);
argList2(i) = argList(i);
aspList2(i) = aspList(i);
cysList2(i) = cysList(i);
gluList2(i) = gluList(i);
hisList2(i) = hisList(i);
lysList2(i) = lysList(i);
tyrList2(i) = tyrList(i);
disp('passed test');
end
end
You can try vectorising the code; I have made up some sample data sets and duplicated some of the operations you're performing below.
matA1 = floor(rand(10)*1000);
matB1 = floor(rand(10)*1000);
matA2 = zeros(10);
matB2 = zeros(10);
minList = [10, 20];
maxList = [100, 200];
indicesToCopy = ( matA1 >= minList(1) ) & ( matA1 <= maxList(1) ) & ( matB1 >= minList(2) ) & ( matB1 <= maxList(2) );
matA2(indicesToCopy) = matA1(indicesToCopy);
matB2(indicesToCopy) = matB1(indicesToCopy);
No idea whether this is any faster, you'll have to try it out.
EDIT:
This doesn't matter too much since you're only making two calls, but xlsread is horribly slow. You can speed up those calls by using this variant syntax of the function.
num = xlsread(filename, sheet, 'range', 'basic')
The catch is that the range argument is ignored and the entire sheet is read, so you'll have to mess with indexing the result correctly.
Use the profiler to see which lines or functions are using the most execution time.
You can probably get a huge increase in execution speed by vectorizing your code. This means using operations which operate on an entire vector at once, instead of using a for-loop to iterate through it. Something like:
% make a logical vector indicating what you want to include
ii = (argList >= minList(1) & argList <= maxList(1)) & ...
% use it
argList2(ii) = arglist(ii); % copies over every element where the corresponding ii is 1
...