What is the immediate lower number next to One in Double precision - matlab

What is the most immediate lower number next to number 1, in double-precision number format ? How to find that in MATLAB?
For instance, the next higher number next to positive number X can be find using X+eps(X). But how to do that for an immediate lower number?

format hex # So that the difference is easy to see
X-eps(X)
appears to work just fine

use the code bellow ,B is most immediate lower number next to number 1 that is half of eps:
A = uint64(0);
bits=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0];
for i = 1: 64
A = bitset(A, i, bits(i));
end
fileID = fopen('bits.bin','w');
fwrite(fileID, A,'uint64');
fclose(fileID);
fileID = fopen('bits.bin');
B = fread(fileID,1,'*float64');
fclose(fileID);
disp(B);
disp(1.0-B);
disp(eps(1.0))

Related

MATLAB accumulation

I seem to be having a lot of difficulty accumulating and then outputting a variable in MATLAB.
Please see the code below. All I want to do is to add the decimal representation to the totalsum. I've tried what feels like a million things. I think I am misunderstanding how to iterate through a MATLAB vector. Seems simple enough.
%clear the command window
clc();
%clear all of the variable stored
clear all;
% close all plots and figures
close();
num = 1.32421875000000000000;
format long g;
%bin_num = dec2bin(num);
fprintf("binary representation of num: %.45f \n", num);
num = num + (1/2^52);
fprintf("the next largest number is: %.45f \n", num);
num = num - (1/2^52);
%bin = '0011111111110101001011111111111111111111111111111111111111111111'
sign = '0';
exponent = '0 1 1 1 1 1 1 1 1 1 1';
decexponent = bin2dec(exponent);
mantissa = [0 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
for i = 1:1:length(mantissa)
sumtotal = sumtotal + mantissa(i)*(1/(2^i)));
end
%num = (-1)^bin2dec(sign)*(2^(decexponent-1023))*(1 + sum);
fprintf("the next smallest number is: %.45f \n", (sumtotal));
There were some errors. I removed the errors and it worked.
Output is as follow
binary representation of num: 1.324218750000000000000000000000000000000000000
the next largest number is: 1.324218750000000222044604925031308084726333618
the next smallest number is: 0.324218749999999777955395074968691915273666382
Use the below code and please tell me do you want to do this ..
%clear the command window
clc();
%clear all of the variable stored
clear all;
% close all plots and figures
close();
num = 1.32421875000000000000;
format long g;
%bin_num = dec2bin(num);
fprintf("binary representation of num: %.45f \n", num);
num = num + (1/2^52);
fprintf("the next largest number is: %.45f \n", num);
num = num - (1/2^52);
%bin = '0011111111110101001011111111111111111111111111111111111111111111'
sign = '0';
exponent = '0 1 1 1 1 1 1 1 1 1 1';
decexponent = bin2dec(exponent);
sumtotal=0;
mantissa = [0 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
for i = 1:1:length(mantissa)
sumtotal = sumtotal + mantissa(i)*(1/(2^i));
end
%num = (-1)^bin2dec(sign)*(2^(decexponent-1023))*(1 + sum);
fprintf("the next smallest number is: %.45f \n", (sumtotal));

how to produce every permutation of positioning 20 values of -1 in a 1-by-41 vector of ones?

I have written different code to produce different permutations of ones and minus ones. they work for matrixes with small dimensions:
for example:
S=[-1 -1 1 1 1 1 1 1];
P=unique(perms(S),'rows');
produces:
-1 -1 1 1 1 1 1 1
-1 1 -1 1 1 1 1 1
-1 1 1 -1 1 1 1 1
-1 1 1 1 -1 1 1 1
-1 1 1 1 1 -1 1 1
-1 1 1 1 1 1 -1 1
-1 1 1 1 1 1 1 -1
1 -1 -1 1 1 1 1 1
1 -1 1 -1 1 1 1 1
1 -1 1 1 -1 1 1 1
1 -1 1 1 1 -1 1 1
1 -1 1 1 1 1 -1 1
1 -1 1 1 1 1 1 -1
1 1 -1 -1 1 1 1 1
1 1 -1 1 -1 1 1 1
1 1 -1 1 1 -1 1 1
1 1 -1 1 1 1 -1 1
1 1 -1 1 1 1 1 -1
1 1 1 -1 -1 1 1 1
1 1 1 -1 1 -1 1 1
1 1 1 -1 1 1 -1 1
1 1 1 -1 1 1 1 -1
1 1 1 1 -1 -1 1 1
1 1 1 1 -1 1 -1 1
1 1 1 1 -1 1 1 -1
1 1 1 1 1 -1 -1 1
1 1 1 1 1 -1 1 -1
1 1 1 1 1 1 -1 -1
or
indices = nchoosek(1:41, 6);
N = size(indices, 1);
S = ones(N, 41);
S(sub2ind([N 41], [1:N 1:N 1:N 1:N 1:N 1:N].', indices(:))) = -1;
can produce
a matrix of 4496388_by_41 of all the permutations of 6 minus one(-1) and 35 one(1).
these codes work for smaller dimensions but they don't work for the matrixs with larger dimensions.
my goal is to produce all permutations of 20 minus one(-1) and 21 one(1) this matrix has 269128937220 rows and 41 columns. but the following codes don't work:
indices = nchoosek(1:41, 20);
N = size(indices, 1);
S = ones(N, 41);
S(sub2ind([N 41], [1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N 1:N].', indices(:))) = -1;
or
S=[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
P=unique(perms(S),'rows');
I do a simple calculation on each permutation(each row of this matrix). if I could write each row of this matrix with for loops and then do the calculation on that row, I would be able to keep the best result and in this situation I wouldn't have to keep all these data in the memory and I wouldn't get out of memory errors from matlab.
if you know how to produce a matrix of all the permutations of 20 minus one(-1) and 21 one(1) with for loops or any other way to store them in my computer please help.
thanks in advance
I'm not an expert in Matlab so I can't speak for all of the resources available, however, I know that your task is feasible on a standard laptop without any fancy high performance services such as https://aws.amazon.com/hpc/.
I have authored a package in R called RcppAlgos that is capable of completing this task comfortably in a few hours. Here is the code:
options(scipen = 999)
library(parallel)
library(RcppAlgos)
## WARNING Don't run this unless you have a few hours on your hand
## break up into even intervals of one million
firstPart <- mclapply(seq(1, 269128000000, 10^6), function(x) {
temp <- permuteGeneral(c(1L,-1L), freqs = c(21,20), lower = x, upper = x + 999999)
## your analysis here
x
}, mc.cores = 8)
## get the last few results and complete analysis
lastPart <- permuteGeneral(c(1L, -1L), freqs = c(21, 20),
lower = 269128000000, upper = 269128937220)
## analysis for last part goes here
And to give you a demonstration of the efficiency of this setup, we will demonstrate how fast the first one billion results are completed.
system.time(mclapply(seq(1, 10^9, 10^6), function(x) {
temp <- permuteGeneral(c(1L, -1L), freqs = c(21, 20), lower = x, upper = x + 999999)
## your analysis here
x
}, mc.cores = 8))
user system elapsed
121.158 64.057 27.182
Under 30 seconds for 1000000000 results!!!!!!!
So, this will not take over 3000 days as #CrisLuengo calculated but rather a conservative estimate of 30 seconds per billion gives :
(269128937220 / 1000000000 / 60) * 30 ~= 134.5645 minutes
I should also note that with the setup above you are only using 1251.2 Mb at a time, so your memory will not explode.
testSize <- object.size(permuteGeneral(c(1L,-1L), freqs = c(21,20), upper = 1e6))
print(testSize, units = "Mb")
156.4 Mb ## per core
All results were obtained on a MacBook Pro 2.8GHz quad core (with 4 virtual cores.. 8 total).
Edit:
As #CrisLuengo points out, the above only measures generating that many permutations and does not factor in the time taken for analysis of each computation. After some more clarification and a new question, we have that answer now... about 2.5 days!!!

Generating a matrix containing 3 numbers

I am trying to create an 8x8 matrix containing 0s, 1s and 2s. Each row and each column should contain two 0s, three 1s and three 2s.
Previously I have used the below to generate an example containing only 1s and 0s.
output = zeros(8, 8);
for i=1:8
tmp = (1:8) + (i);
tmp = rem(tmp, 4);
output(i,:) = tmp;
output(i,:) = tmp > 0;
end
output =
1 1 0 1 1 1 0 1
1 0 1 1 1 0 1 1
0 1 1 1 0 1 1 1
1 1 1 0 1 1 1 0
1 1 0 1 1 1 0 1
1 0 1 1 1 0 1 1
0 1 1 1 0 1 1 1
1 1 1 0 1 1 1 0
However I would now like something similar to the following:
output =
1 1 0 1 2 2 0 2
1 0 1 2 2 0 2 1
0 1 2 2 0 2 1 1
1 2 2 0 2 1 1 0
2 2 0 2 1 1 0 1
2 0 2 1 1 0 1 2
0 2 1 1 0 1 2 2
2 1 1 0 1 2 2 0
Thanks for your help.
What you have in your example is a Hankel matrix so you could use the hankel function
c = [1 1 0 1 2 2 0 2];
k = [2 1 1 0 1 2 2 0];
A = hankel(c,k)
where c is the first column of the output matrix and k is the last row.
Making your output matrix a Hankel matrix is a good idea (based on your requirements) as it will enforce the row and column frequency counts for each value. You would not necessarily get this just by creating rows that are random permutations of a base row (using randperm for example) as duplicate rows would be possible which would break your column requirements.
As an example, if you want random c with fixed numbers of specific elements, you can randomly permute a base vector containing the required values and frequencies - as per your requirement this would be
c = [0 0 1 1 1 2 2 2];
index = randperm(numel(c));
c = c(index);
c =
0 2 0 2 2 1 1 1
To get the square Hankel structure then choose k to be the next cyclic permutation of c
k = circshift(c',1)'
k =
1 0 2 0 2 2 1 1
and just call hankel with these as mentioned above
A = hankel(c,k)
A =
0 2 0 2 2 1 1 1
2 0 2 2 1 1 1 0
0 2 2 1 1 1 0 2
2 2 1 1 1 0 2 0
2 1 1 1 0 2 0 2
1 1 1 0 2 0 2 2
1 1 0 2 0 2 2 1
1 0 2 0 2 2 1 1
The above output is based on what I got on my machine based on the output from randperm.
Any output matrix generated using the above will meet your requirements specified in the question.

How to find time delay between two time series data?

I want to know how to find delay between two time series, for example when we have this pseudo time series data:
A: 1 1 1 1 1 2 1 1 1 1
B: 1 1 1 1 2 1 1 1 1 1
C: 1 1 2 1 1 1 1 2 1 1
For example:
A: 1 1 1 1 1 2 1 1 1 1
B: 1 1 1 1 2 1 1 1 1 1
clear all;
filename = 'pseudo.xls';
visi=xlsread(filename); %nuskaitomi visi stulpeliai
[n,m]=size(visi);
A=visi(:,1);
B=visi(:,2);
C=visi(:,3);
A1=transpose(A);
B1=transpose(B);
C1=transpose(C);
t=(1:10);
plot(t,A1,'b'); hold on;
plot(t,B1,'m');
d1 = finddelay(B1,A1);
[c,lags] = xcorr(B1,A1);
d2 = -(lags(c == max(c)));
And the answer is d1=1 and d2=1. It all right.
But what about the time delay between B and C.
B: 1 1 1 1 2 1 1 1 1 1
C: 1 1 2 1 1 1 1 2 1 1
How to find two delay parameters?
And how it works when we have two long time series?
Perhaps you can offer and more methods of determining the time delay?
Thank you for your advice and answers :)

How to do multilevel indexing on array of structs in Matlab?

Let's say I create an array of structs in matlab using:
mystruct = repmat(struct('field1',1, 'field2', ones(10,1)), 10, 1 );
For my purposes (simple example aside), I would find it very useful to get a vector output from using:
myvector = mystruct(:).field2(1)
However this gives me the error:
'Scalar index required for this type of multi-level indexing.'
EDIT: What I expect to get is the first element of the ones vector, from each struct in the array, hence a 10x1 vector of '1'.
I could easily manually using a for loop go through each value in my struct and assign to myvector but that seems incredibly cumbersome and also slow. Any thoughts?
I'm assuming you're trying to collect all of the field2 vectors into myvector:
myvector = [mystruct(:).field2];
Returns:
myvector =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
EDIT: Per your comment, you can use the above and throw out the data you don't want (myvector(2:end,:) = []; in this case). This is a pretty memory intensive way to do it though. There may be a way to pull what you want using structfun or similar but I'd need to think about how to do it.
EDIT2: Try arrayfun(#(x) x.field2(1), mystruct) and see if this returns what you're looking for.
In two step you can:
get your struct filed2 as a matrix:
foo = [mystruct.field2];
get the first row (that contains the first indices of the field2)
myvector = foo(1, :);