MATLAB accumulation - matlab

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));

Related

delete rows in matrix in conditions in matlab

My program creates a matrix whose cell values ​​in multiple rows are the same in the corresponding column. I want to delete some rows that have 0 more than one. To clarify, my matrix has the following form,
A=[ 1 1 1 0 0 1 1 1; 1 0 0 1 1 1 1 1 1; 1 1 1 1 1 1 1 0; 1 1 1 1 0 1 1 1
1 1 0 1 0 0 1 1 ]
and I want to delete all the columns that are in the first, second and fifth rows because the number 0 is 2
or more left in the matrix of rows that are in the third and fourth rows because they have 0 one in each row.
The result should be the following matrix:
A=[ 1 1 1 1 1 1 1 1 0; 1 1 1 1 0 1 1 1 ]
i write this code for your algorithm than work correctly:
% Input Matrix
A = [1 1 1 0 0 1 1 1;1 0 0 1 1 1 1 1; 1 1 1 1 1 1 1 0;1 1 1 1 0 1 1 1;1 1 0 1 0 0 1 1 ];
% find number of rows and cols
[num_rows, num_cols] = size(A);
% Itrate on each row and find rows that have less than 2 zeros
selected_rows = [];
idx = 1;
for i=1:num_rows
num_zero = sum(A(i, 1:end) == 0);
if num_zero < 2
selected_rows(idx) = i;
idx = idx+1;
end
end
% return result matrix
result = [];
for i=1:length(selected_rows)
result = [result; A(selected_rows(i), 1:end)];
end
disp(result)

Filling up gaps in matrix's rows

Let g be a matrix containing ones\zeroes. I want to fill up gaps of zeros (complete ones sequences) in the rows of g which are smaller then a given k.
For example fill up with ones all gaps smaller then three zeros. This code will work:
[m,n]=size(g);
k=3
for i=1:m
j=1
while (j<n)
if(g(i,j)==0)
flag=0;
for w=1:k
if(g(i,j+w-1)==1)
flag=1;
end
end
if(flag)
for w=1:k
g(i,j+w-1)=1;
end
else
while(~flag&j<n)
j=j+1;
if(g(i,j)==1)
flag=1;
end
end
end
end
j=j+1;
end
end
Is there a way to do so without all the for loops?
A short version using some built-in function:
M = [1 1 1 1 1
1 1 1 0 0
1 1 1 0 1
1 1 0 0 0
0 1 1 1 1]
Mopen = ~imopen(~padarray(M,[0,1],1),strel('line',3,0));
Mfill = Mopen(:,2:end-1);
A vectorized version:
k = 3;
d = diff( g,1,2);
L = d ~= 0;
c = cumsum([zeros(size(g,1),1) L],2)+1;
b = bsxfun(#plus, c, cumsum([0; c(1:end-1,end)]));
a =accumarray(reshape(b.',[],1),1);
f= find(a<k);
g(ismember(b,f) & g==0) = 1;
Example :
g =
1 1 1 1 1
1 1 1 0 0
1 1 1 0 1
1 1 0 0 0
0 1 1 1 1
result =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 0 0 0
1 1 1 1 1
Another potential solution is to cast each row to a char array and then perform substitutions based on a regular expressions
g = round(rand(30,70));
figure();
subplot(121);
imagesc(g)
[m,n]=size(g);
k=3;
pattern = ['(?<!',char(0),')(',char(0),'{1,',num2str(k-1),'})(?!',char(0),')'];
for i=1:m
[matchstart,matchend] = regexp(char(g(i,:)),pattern);
for j = 1:length(matchstart)
g(i,matchstart(j):matchend(j)) = 1;
end
end
subplot(122);
imagesc(g)

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

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))

Split Matrix into several depending on value matlab

I am trying to split an nx3 matrix into submatrices in matlab. So my matrix C is of this shape
C =
1 1 1
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
0 0 0
1 1 1
1 1 1
It either has rows of zeros or rows of ones. I want to split this matrix to only keep matrices of ones. So here for instance there's three 'groups' of ones. so I want to get
C1 =
1 1 1
C2 =
1 1 1
1 1 1
1 1 1
C3 =
1 1 1
1 1 1
However my real matrix is an n by 3, so i don't know where the ones are.
Edit 1:
Now, i need to split x1 and y1 (individual row vectors with same length as C) to x(1), x(2),.. (similarly for y vector) based on how the matrix C was split
Sample Input:
x1 = (1:9)';
y1 = (2:2:18)';
Desired Output:
x(1)=[1], x(2)=[4 5 6]' and x(3)=[8 9]'
y(1)=[2], y(2) =[8 10 12]' and y(3)=[16 18]'
Use find function to the first column to find the indices of 1-elements. Then, calculate the relative distance between any two consecutive elements of the obtained vector to determine the boundary. After that, use matrix indexing to get the blocks of 1-element-matrices. Let me know if you have a better idea.
c=[1 1 1;
0 0 0;
0 0 0;
1 1 1;
1 1 1;
1 1 1;
0 0 0;
1 1 1;
1 1 1;]
b=c(:, 1);
find(b);
I will leave the rest to yourself.
Input:
c = [1 1 1;
0 0 0;
0 0 0;
1 1 1;
1 1 1;
1 1 1;
0 0 0;
1 1 1;
1 1 1;]
x1 = (1:9)';
y1 = (2:2:18)';
Code:
elementalLengthA = cellfun('length',regexp(sprintf('%i',all(C,2)),'1+','match'));
elementalStartA = regexp(sprintf('%i',all(C,2)),'1+','start');
result = cell(length(elementalLengthA),1);
x = cell(length(elementalLengthA),1);
y = cell(length(elementalLengthA),1);
for i = 1:length(elementalLengthA)
result(i) = {C(elementalStartA(i):elementalStartA(i)+elementalLengthA(i)-1,:)};
x(i) = {x1(elementalStartA(i):elementalStartA(i)+elementalLengthA(i)-1,:)};
y(i) = {y1(elementalStartA(i):elementalStartA(i)+elementalLengthA(i)-1,:)};
end
Output:
>> cell2mat(result(1))
ans =
1 1 1
>> cell2mat(result(2))
ans =
1 1 1
1 1 1
1 1 1
>> cell2mat(result(3))
ans =
1 1 1
1 1 1
>> cell2mat(x(3)) %similarly do it for other cells to get results
ans =
8
9

Finding a binary matrix such that the given hamming weight is constant

Given a square binary matrix. I want to get all possible binary matrices which are at d Hamming distance apart.
Suppose
A=[1 0 1;
0 1 1;
1 1 0].
Then a matrix which is one (d) Hamming distance apart is
[0 0 1;
0 1 1;
1 1 0].
Any help in Matlab base coding?
I am hoping that I got the definition of hamming weight right in the given context. Based on that hope/assumption, this might be what you were after -
combs = dec2base(0:2^9-1,2,9)-'0'; %//'# Find all combinations
combs_3d = reshape(combs',3,3,[]); %//'# Reshape into a 3D array
%// Calculate the hamming weights between A and all combinations.
%// Choose the ones with hamming weights equal to `1`
out = combs_3d(:,:,sum(sum(abs(bsxfun(#minus,A,combs_3d)),2),1)==1)
Thus, each 3D slice of out would give you such a 3 x 3 matrix with 1 hamming weight between them and A.
It looks like you have 9 such matrices -
out(:,:,1) =
0 0 1
0 1 1
1 1 0
out(:,:,2) =
1 0 1
0 1 1
0 1 0
out(:,:,3) =
1 0 1
0 0 1
1 1 0
out(:,:,4) =
1 0 1
0 1 1
1 0 0
out(:,:,5) =
1 0 0
0 1 1
1 1 0
out(:,:,6) =
1 0 1
0 1 0
1 1 0
out(:,:,7) =
1 0 1
0 1 1
1 1 1
out(:,:,8) =
1 1 1
0 1 1
1 1 0
out(:,:,9) =
1 0 1
1 1 1
1 1 0
Edit
For big n, you need to use loops it seems -
n = size(A,1);
nsq = n^2;
A_col = A(:).';
out = zeros(n,n,nsq);
count = 1;
for k1 = 0:2^nsq-1
match1 = dec2bin(k1,nsq)-'0';
if sum(abs(match1-A_col))==1
out(:,:,count) = reshape(match1,n,n);
count = count + 1;
end
end