Random number generator in Matlab - matlab

I am generating random sequences of numbers using the same seed:
sprev = rng(2,'v5uniform');
for i=1:N
%do some operations
rndIDX = randperm(sampleSize) ;
newdata= data(rndIDX(1:newSampleSize), :) ;
if x>y
remove=x1; %line 7
end
for l=1:M
%do something else
if l>xy & ~empty(remove)
%do something related to remove
elseif l>xy
%do nothing
else
%do something not related to remove
end
%more code here
end
end
However, when I comment out line 7, rndIDX returns a different sequence of numbers which for me is unexpected. There might be a bug somewhere in the code but I am not sure what is the relationship between the sequence produced by randperm and the code that follows. Also, if i keep the code as presented I always get the same newdata which is the expected behaviour. I just want to comment out line 7 and still get the same newdata.I can confirm that sampleSize is always the same as well as newSampleSize for both cases.

Related

how can I skip some iteration of for loop and do others iteration in Matlab?

I have a program that all the function is in the for loop. I want to skip some numbers of variable in for to don't do any thing if a condition goes true. for example:
for i=1:a
for j=1:b
if counter=101
%until i<21 do nothing then if i=21 continue the loop and do what I want
% until i=23, after that again i goes forward but don't do any thing
%until i=44 again do the progress and continue the loop.
end
%do what I want
end
end
but I can't get the right answer.
I write it like down:
if counter==101
j=1;
while (i < a/3+1)
i=i+1;
continue
end
end
if counter==201
j=1;
while (i < 2*a/3+1)
i=i+1;
continue
end
end
In this code, after i=21 do iteration, after that in for loop i goes to the number before 21(for example 3) but i don't want it.
If anyone has advice, that will be greatly appreciated.
This condition should be what you are looking for:
for i=1:a
for j=1:b
if i < 21 || (i >= 23 && i < 44)
% Skip loop before 21 and between 23 and 43
continue
end
if counter == 101
% Do stuff
end
end
end
There is at least an error in your code: if counter=101 is not a valid comparison, you are assigning the value 101 to counter. You should use if counter==101
Also, I do not understand your second block of code at all. A the very least, you use i before assigning a value to it and an outer loop is probably missing.
Edit:
That being said, you might want to simply adjust your looping variable:
range = [1:20 , 22, 44:a];
for i = range
% Do stuff
end

Changing a code to get the Goldbach conjecture

To test the Golbach conjecture I have found the following code, but when I ren it out, this only gives me the number of combinations of primes, but in fact what I was searching for are the primes,I mean the couple of primes such that I get the even number.
Can someone help me to change the below code in matlab to get the couple of primes?
function y=goldbach_partition(n)
if mod(n,2)==0
y=0;
for i=2:floor(n/2)
if isprime(i) & isprime(n-i)
y=y+1;
end;
end;
end;
Thanks a lot in advance :)
That's pretty simple. Create a new array that will store the prime number i (or n-i... can't say for sure) in an array should the value i (and n-i) pass the criterion, then return that array. BTW, you should move your y=0 statement outside of the if statement, because if n is odd, you will never see y being output and you'll get an error.
Something like this:
function [y,pmes]=goldbach_partition(n) %// Change
y = 0; %// Change
pmes = []; %// Change
if mod(n,2)==0
for i=2:floor(n/2)
if isprime(i) & isprime(n-i)
y=y+1;
pmes = [pmes; i n-i]; %// Change
end
end
end
I'm not sure which prime number you're after - if it's i or n-i, so I've added them both. I've also made sure that both primes should they meet the condition be placed in a single row of your matrix. This way, each row will give you when a pair of primes has passed the condition.
Therefore, the above code will not only return the total number of primes, it'll return an array of all of the primes that passed the condition.
You would then call your function this way in MATLAB:
[y,pmes] = goldbach_partition(n);

How to delete decimals that have repeating decimals

I'm working on a code able to work data from star of a public catalogue. I've already got arrays of every single variable that is available:
enter code here fid=fopen('000006+2553.txt','r');
i=1;
while 1
tline=fgetl(fid);
if ~ischar(tline), break, end
A{i}=tline;
i=i+1;
end
k=1;
for j=1:1:length(A)
if length(A{j}) > 50 && length(A{j})<=92
B{k}=A{j};
k=k+1;
end
end
m=1;
for l=1:1:length(B)
C=strread(B{l},'%s','delimiter',' ');
HJD(m)=str2num(C{1});
MAG_3(m)=str2num(C{2});
MAG_0(m)=str2num(C{3});
MAG_1(m)=str2num(C{4});
MAG_2(m)=str2num(C{5});
MAG_4(m)=str2num(C{6});
MER_3(m)=str2num(C{7});
MER_0(m)=str2num(C{8});
MER_1(m)=str2num(C{9});
MER_2(m)=str2num(C{10});
MER_4(m)=str2num(C{11});
FRAME(m)=str2num(C{13});
m=m+1;
end
My problem is, that some of the values in the arrays are repeating decimals, like 29.99999 and 99.99999....etc. Since this numbers are the result of saturation of the sensor, they are wrong data, and must be eliminated. Is there anyway I can tell MATLAB to delete this particular numbers? Any help would be appreciated.

continue statment not executing correctly within for loop+matlab

I have the following code:
for i=1:length(files)
for j=1:length(files(i,1).day)
if ((1:length(files(i,1).day(j).hour)) ~= 24)
continue
end
for h = 1:24
if ((1:length(files(i,1).day(j).hour(h).halfhour)) ~= 2)
continue
end
if isempty(1:length(files(i,1).day(j).hour))
continue
end
dayPSD = [];
for h2=1:2
dayPSD = [dayPSD; files(i,1).day(j).hour(h).halfhour(h2).data{8}'];
end
end
end
end
where
files=1x3 structure
files(i,1).day=1x335 structure that contains 1 field hour which is a 1x24 structure
files(i,1).day.hour=1x24 structure that contains 1 field halfhour which is a 1x2 structure
I am trying to loop through i and j, but when 1:length(files(i,1).day(j).hour) ~= 24 (so when the hours are less than 24) I would like to exit the loop and continue onto the next iteration of the for loop. I would also like it to exit the loop and continue onto the next iteration of the for loop if 1:length(files(i,1).day(j).hour(h).halfhour) ~= 2 (so when the half hours are less than 2) or if 1:length(files(i,1).day(j).hour is an empty cell (no data).
The current code I have written only seems to skip a loop index if 1:length(files(i,1).day(j).hour ~=24.
What am I doing wrong? Also how do I get it to continue onto the next iteration if any of those conditions are met? I've been banging my head on this for a while, and I know I'm probably making a simple mistake, but if anyone can point me in the right direction that would be great.
Thanks for your help in advance!
If I understand correctly, what you want is:
for i=1:length(files)
for j=1:length(files(i,1).day)
if ((1:length(files(i,1).day(j).hour)) ~= 24)
continue
end
for h = 1:24
if ((length(files(i,1).day(j).hour(h).halfhour)) ~= 2)
break
end
if isempty(files(i,1).day(j).hour)
break
end
dayPSD = [];
for h2=1:2
dayPSD = [dayPSD; files(i,1).day(j).hour(h).halfhour(h2).data{8}'];
end
end
end
end
so when either condition is met, the flow of execution will exit the for h=.. loop, and as there is no other statement in the for j=1... loop, the code will in effect skip that iteration.
Also note that if ((1:length(files(i,1).day(j).hour(h).halfhour)) ~= 2) is comparing a vector (1:length(..)) to a scalar 2 which results in a vector of booleans. The if part will only be executed if all its elements are true which is most likely never the case.

How to check a matrix is/isn't in an array (Matlab)

I have an array (M) of matrices. I perform an operation on the matrix in the ith position, and it adds three more matrices to my array in the (3i-1), (3i) and (3i+1)th positions. I want to continue this process until I reach the jth position in the array, where j is such that all matrices in the (j+1)th position and onwards have appeared already somewhere between positions 1 and j (inclusive).
EDIT: I've been asked to clarify what I mean. I am unable to write code that makes my algorithm terminate when I want it to as explained above. If I knew a proper way of searching through an array of matrices to check if a given matrix is contained, then I could do it. I tried the following:
done = 0;
ii = 1
while done ~= 1
%operation on matrix in ith position omitted, but this is where it goes
for jj = ii+1:numel(M)
for kk = 1:ii
if M{jj} == M{kk};
done = done + 1/(numel(M) - ii);
break
end
end
end
if done ~= 1
done = 0;
end
ii = ii + 1
end
The problem I have with this (as I'm sure you can see) is that if the process goes on for too long, rounding errors stop ever allowing done = 1, and the algorithm doesn't terminate. I tried getting round this by introducing thresholds, something like
while abs(done - 1) > thresh
and
if abs(done - 1) > thresh
done = 0;
end
This makes the algorithm work more often, but I don't have a 'one size fits all' threshold that I could use (the process could continue for arbitrarily many steps), so it still ends up breaking.
What can I do to fix this?
Thanks
Why don't you initialize done at 0, keep your while done==0 loop, and instead of computing done as a sum of elements, check if your condition (finding if the matrix already exists) is verified for all jj, something like this:
alldone=zeros(numel(M)-ii,1);
for jj = ii+1:numel(M)
for kk = 1:ii
if isequal(M{jj},M{kk})
alldone(jj-ii) = 1
break
end
end
end
done=prod(alldone);
There is probably a more elegant way to code this, though.
For instance, you could add early termination:
while done==0
done=1;
for jj = ii+1:numel(M)
match_success=0;
for kk = 1:ii
if isequal(M{jj},M{kk})
match_success=1;
break
end
end
if match_success==0
done=0;
break;
end
end
end
At the beginning of each loop, the algorithm assumes it is going to succeed and stop there (hence the done=1). Then for each jj, we create a match_success which will be set to 1 only if a match is found for M{jj}. If the match is found, we break and go to the next j. If no match if found for j, match_success is left to 0, done is initialized to 0 and the while loop continues. I haven't checked it, but I think it should work.
This is just a simple tweak, but again, more thought can probably speed up this whole code a lot.