matrix dimensions wont agree when elements match/matlab - matlab

I am having a problem at the final calculation of my code, the very last part, where log is the natural log, I need RD=facs.*log(log(facs)) to divide sigmafac, or robin=sigmafac./RD. My RD goes from 1 to 100, so does my sigmafac. why is there a matrix dimension mismatch?
I want the corresponding number (numbas) of RD to divide the correspoding number of sigmafac, the all have the same dimension, so I do not see where the problem is coming from. I realize that RD(1)=-inf, is that is what causing the problem? and how do I fix it?
code:
n=100;
primlist=2; % starting the prime number list
for numba=1:n;
if mod(2+numba,primlist)~=0
primlist=[primlist;2+numba]; %generating the prime number list
end
end
fac=1; %initializing the factorials
RD=0;
for numbas=2:n
%preallocating vectors for later use
prims=zeros(size(primlist));
pprims=zeros(size(primlist));
pow=prims;
for i=1:length(primlist) % identifying each primes in the primlist
for k=1:10
if mod(numbas,primlist(i).^k)==0
prims(i)=primlist(i); % sum of all the powers of prims, such that prims divide numbas
pow(i)=k; % collecting the exponents of primes
end
end
if primlist(i)<=numbas
pprims(i)=primlist(i); % primes less than or equal to numbas
end
end
% converting column vectors to row vector
PPRIMS=pprims';
PRIMS=prims';
POW=pow';
%Creating the vectors
PLN(numbas,:)=PPRIMS; % vector of primes less than or equal to number
PPV(numbas,:)=PRIMS; % prime divisor vector
PVE(numbas,:)=POW; % highest power of each primes for every number
RVE=cumsum(PVE); % the cummulative sum of the exponents
RVE(RVE~=0)=RVE(RVE~=0)+1; %selects each non zero element then add 1
%factorial
fac=fac*numbas;
facs(numbas)=fac; %storing the factorials
if facs==1
RD==1; % log(log(facs1))) does not exist
else RD=facs.*log(log(facs));
end
end
% setting up sum of divisor vector
NV=PLN.^RVE-1; % numerator part of sum of divisors vector
DV=PLN-1; % denominator part of sum of divisors
NV(NV==0)=1; % getting rid of 0 for elementwise product
DV(DV==-1)=1; % getting rid of -1 for elementwise product
sigmafac=prod(NV,2)./prod(DV,2); %sum of divisors
robin=(sigmafac)./(RD)

Whenever you get such an error, your first check should be to test
size(sigmafac)
size(RD)
In this case, you'll get
ans =
100 1
ans =
1 100
So they are NOT the same size. You need to take the transpose of one or the other and then your division will work fine.

Your sigmafac is 100x1 but your RD is 1x100 which is producing the error. If you want this to work just change
robin=(sigmafac)./(RD)
to
robin=(sigmafac)'./(RD)
This will make sigmafac a 1x100 (transpose) and then your vectors will have the same dimension and you will be able to do the division.

Related

How to show that randperm() in matlab is fair

Suppose, I wanted to show (empirically) that randperm(n,k) from matlab indeed produces uniformly distributed random samples of size k from a set N of n elements. How can I plot the number of occurences divided by the total number of k-subsets drawn from N, after drawing repeatedly?
You can simply use the indices drawn from randperm to increment a counter vector.
n=1e5;
k=1e4;
maxiter = 1e5;
% This array will be used to count the number of times each integer has been drawn
count=zeros(n,1);
for ii=1:maxiter
p=randperm(n,k);
% p is a vector of k distinct integers in the 1:n range
% the array count will be incremented at indices given by p
count(p)=count(p)+1;
end
% A total of k*maxiter integers has been drawn and they should be evenly
% distributed over n values
% The following vector should have values close to 1 for large values of maxiter
prob = count*n/(k*maxiter);

Exlude some values in a matrix then compute the variance

I have a matrix X which has dimension 1000*16001. I want to exlude any values in this matrix that are greater than or equal 1 or smaller than or less than -1. Then computing the variance over the second dimension. My code is
[L,K]=size(X);
for n=1:K
for m=1:L
if (X(m,n)>=1 || X(m,n)<=-1)
X(m,n)=NaN;
end
varx=var(X(:,n),1,1,'omitnan');
end
end
Then plot the variance.
plot(T,varx','k','linewidth',2)
where T is time which has dimension 16001*1. However I get this error
Too many input arguments
varx=var(X(:,n),1,1,'omitnan');
How can fix this error and plot the variance over the time T. Many thank for your help.
The following should delete all the values greater than 1 or smaller than -1:
Xnew = X;
Xnew(X > 1 | X < -1) = NaN;
Next, the variance along each row can be calculated using:
varx = var(Xnew,[],2,'omitnan’)
The plot, then, should not be a problem for you:
plot(T,varx,'k','linewidth',2)

Vectors must be the same length error

I'm trying to plot an integral L, with respect to Xdot, but I keep getting vectors must be the same length error, I don't know how to fix it however. My code is shown below. You can see at the bottom, the loop for the sum only goes up to 99, while the x_1 goes up to 100. If I change the loop however, I get an error "index exceeds matrix dimensions"
% The solution for this part is based on the Euler method
f=0; %initializing the force row vector
f_1=0.5; %initializing the first derivative of force
x=1; % intializing the mass displacement%
x_1(1)=0; % initializing the first derivative of mass displacement
t=0; % initializing the time row vector
j=1; % initializing a`enter code here`n index used in iterations
a=0;
b=10;
N=100;
h=(b-a)/N;
for j = 0:N-1
f_2=-1*sin(f); %obtain the second derivative of the force
f_1=f_1+f_2*h; %obtain the first derivative of the force
f=f+f_1*h; % obtain the value of force and concatenate it with
%preceding force row vector the element
x_2=f-0.1*x_1-x-x^3; %obtain the second derivative of the mass displacement
x_1=x_1+x_2*h; % obtain the first derivative of the mass displacement
x=x+x_1*h; % obtain the current value of mass displacement and
%concatenate it with preceding mass displacement row vector the element
t=t+h; % obtain the current value of time and concatenate it with
%preceding time row vector the element
j=j+1; %increment the index iterator by one
v(j)=x;
w(j)=t;
m(j)=x_1
end
sum = 0; %%Trapezoidal method to find L, sum is L, put this at the end of your script
for i = 1:size(m,2)-1
sum = sum + h*(m(i+1)^2+m(i)^2)/2;
L(i) = sum
end
plot (m,L, 'r')
How about you shorten your x by one when plotting?
plot (m(1:end-1),L, 'r')

Matlab sums and integers

Thank you for any help in advance. I have a large matrix: 1,000,000 rows and 10 columns. I would like to sum each row and create a new matrix with only the rows that sum to integers. I've tried this so far and manipulated it in many ways, but I'm stuck. How can I do this?
for k = 1:1000000
x = sum(A(k,:)) %A is my large matrix
if x-round(x,0)==0
y = [y;x]% y is my new matrix
end
end
Rather than using a for loop and continuously expanding y which is going to be extremely slow for large x arrays, you can use the second input of sum to compute the sum for each row, and then you can determine which rows sum to an integer by comparing the rounded and original versions using a very small epsilon (the proper way to compare floating-point numbers).
% Sum each row and divide by 3
row_sums = sum(x, 2) / 3;
% Determine which of the row-wise sums are integers
sum_is_integer = abs(round(row_sums) - row_sums) < eps;
% If you want the sums that were integers
y = row_sums(sum_is_integer);
% If you want a sub-matrix containing only the rows where the sums were an integer
z = x(sum_is_integer, :);

Sum numbers and finding maximum, Matlab

I want to sum the powers (r) of primlist (primes) that divides a number, and also pick out the greatest power (k) that makes the primlist divides a number. I believe I have the right concept but matlab is overlooking something in the loop. example when numbas=45, we know 3^2*5=45 so primlist = 3 and 5, for 3: k=2, r=1,3 and for 5, k=1, r=1. r is simply all the powers of a primlist that divides a numba, and k is the highest value of r. i want to sum all the r's and also get the maximum r which is k
n=100;
primlist=2;
for numba=1:n;
if mod(2+numba,primlist)~=0
primlist=[primlist;2+numba];
end
end
prims=reshape(primlist.',1,[]);
r=1;
for numbas=2:n
for k=1:10
if mod(numbas,prims.^k)==0
r=r+sum(k) % sum of all the powers of prims, such that prims divide numbas
k=max(k) % greatest power of prims, such that prims divide numbas
end
end
end
numbas
prims
k
r
I think this does what you are looking for. I added an extra loop to test whether primes divide numbas one-by-one, rather than all at once. r contains the primes that divide numbas and pow contains the corresponding numbers of each. You may want to change how the results are saved, but I think this gives you what you were after.
n=100;
primlist=2;
for numba=1:n;
if mod(2+numba,primlist)~=0
primlist=[primlist;2+numba];
end
end
kMax=0;
for numbas=2:n
r=zeros(size(primlist));
pow=r;
for k=1:10
for i=1:length(primlist)
if mod(numbas,primlist(i).^k)==0
r(i)=primlist(i); % sum of all the powers of prims, such that prims divide numbas
pow(i)=k;
if k>kMax,kMax=k;end % greatest power of prims, such that prims divide numbas
end
end
end
R=r'
POW=pow'
end