There is one equation has 5 variable parameters. Each parameter has one for loop so this equation depends on the 5 for loops and i want to appear the results of this equation in matrix array. How can i do it?
for m1= 1:m1_end;
m1_vec=zeros(1,col_no/m1_end)+m1;
input_mat(1,col_no/m1_end*(m1-1)+1:col_no/m1_end*m1)= m1_vec;
for m2= 1:m2_end;
m2_vec=zeros(1,col_no/m2_end^2)+m2;
input_mat(2,col_no/m2_end^2*(m2-1)+1:col_no/m2_end^2*m2)= m2_vec;
for k1=1:k1_end;
k1_vec=zeros(1,col_no/k1_end^3)+k1;
input_mat(3,col_no/k1_end^3*(k1-1)+1:col_no/k1_end^3*k1)= k1_vec;
for k2=1:k2_end;
k2_vec=zeros(1,col_no/k2_end^4)+k2;
input_mat(4,col_no/k2_end^4*(k2-1)+1:col_no/k2_end^4*k2)= k2_vec;
for k3=1:k3_end;
input_mat(5,col_no/k3_end^5*(k3-1)+1:col_no/k3_end^5*k3)= k3;
M=[m1 0 ; 0 m2 ];
K=[k1+k2 -k2 ; -k2 k2+k3];
eigen_values=sqrt(eig(inv(M)*K))
end
end
end
end
end
How to show the all results of eigen_values in matrix ?
Your code is a bit messy, but you could start with something to hold all eigen_values
Either a matrix (if it is just a scalar) or a cell array if it is a vector.
Then each time after calculating eigen_values assign it to the correct place in your storage variable.
For example, if it is a scalar, and you track your progress with t:
all_eigen_values(t) = eigen_values;
Related
I'm starting with Python and I have a basic question with "for" loop
I have two array which contains a values of a same variables:
A = data_lac[:,0]
In the first array, I have values of area and in the second on, values of mean depth.
I would like to find a way to automatize my calculation with different value of a parameter. The equation is the following one:
g= (np.sqrt(A/pi))/n
Here I can calculte my "g" for each row. Now I want to have a loop with differents values of "n". I did this:
i=0
while i <= len(A)-1:
for n in range(2,6):
g[i] = (np.sqrt(A[i]/pi))/n
i += 1
break
In this case, I just have one column with the calculation for n = 2 but not the following one. I tried to add a second dimension to my array but I have an error message saying that I have too many indices for array.
In other, I would like this array:
g[len(A),5]
g has 5 columns each one calculating with a different "n"
Any tips would be very helpful,
Thanks
Update of the code:
data_lac=np.zeros((106,7))
data_lac[:,0:2]=np.loadtxt("/home...", delimiter=';', skiprows=1, usecols=(0,1))
data_lac[:,1]=data_lac[:,1]*0.001
#Initialisation
A = data_lac[:,0]
#example for A with 4 elements
A=[2.1, 32.0, 4.6, 25]
g = np.zeros((len(A),))
I believe you share the indexes within both loops. You were increasing the i (index for the upper while loop) inside the inner for loop (which index with n).
I guess you have A (1 dim array) and you want to produce G (2 dim array) with size of (Len(A, 5))
I am not sure I'm fully understand your require output but I believe you want something like:
i=0
while i <= len(A)-1:
for n in range(2,6):
g[i][n-2] = (np.sqrt(A[i]/pi))/n # n-2 is to get first index as 0 and last as 4
i += 1 # notice the increace of the i is for the upper while loop
break
Important - remember that in python indentation means a lot -> so make sure the i +=1 is under the while scope and not indent to be inside the for loop
Notice - G definition should be as:
g = np.zeros((len(A),4), dtype=float)
The way you define it (without the 4) cause it to be 1 dim array and not 2-dim
I have created a cell array of dimensions 1x10, named A. Each element contains a 100x5 matrix. Hence, I got 10 matrices 100x5. However, I want to put every matrix of the cell array into a loop. If B is a 100x5 matrix, C is a 100x1 vector and c is a constant, the loop should look like:
for t=1:100;
j=1:5;
x=c*inv((B(t,j)-A(t,j))*((B(t,j)-A(t,j))')*(A(t,j)-C(t,1)*ones(1,5));
end;
end;
At the end x should deliver a 1x10 cell array that will contain 10 elements of matrices 100x5.
I would appreciate any help. Thank you in advance!
If I understand your question correctly, you are asking how to access a cell array. Let i index the cell array. Then you can access the ith entry of the cell array by calling A{i}. Then your code is:
for i=1:10
for t=1:100
j=1:5
x{i}=c*inv((B(t,j)-A{i}(t,j))*((B(t,j)-A{i}(t,j))')*(A{i}(t,j)-C(t,1)*ones(1,5));
end
end
end
You may want to think about your problem and whether or not you can eliminate the the two middle for loops by writing it in matrix notation. It looks similar to a least-squares estimator, which is (X'X)^(-1)*X'y, but the element-by-element inverse is throwing me off.
I have a matrix M:
M=rand(n,m)
and an array K of length m^n
K=zeros(m^n,1)
The array K is filled in with the values obtained by summing up the values of the matrix M across n rows for all possible m^n vertical combination of cells. An auxiliary array Index of length m^n contains index references to the cell combinations so that the column indices of the Index array indicate the rows of matrix M and the values of the Index array – the columns of the matrix M. In the example below for 4x3 matrix, the second row {1,1,1,2} of the Index array corresponds to the combination of cells M(1,1), M(2,1),M(3,1) and M(4,2) etc:
Index =
1 1 1 1
1 1 1 2
1 1 1 3
1 1 2 1
…
This index referencing is then used to calculate the values (sum) of each cell combination which are stored in K:
for i=1:m^(n)
for j=1:n
K(i)= K(i)+M(j,Index(i,j))
end
end
Until this point it works fine. What I need, however, is to introduce an “if” condition so that if any of the cell of matrix M is equal to zero, than the value of any combination containing this zero-value (cell(s)) will be also zero. I have tried to introduce it within the for-loop:
Assume
M(1,1)=0
M(3,1)=0
Then
for i=1:m^(n)
for j=1:n
if M(j,Index(i,j))~=0
K(i)= K(i)+M(j,Index(i,j))
else
K(i)=0
end
end
end
This solution does not seem to work because it fails to identify the cell combinations associated with zero-value cells and I cannot figure out the way around it. Does anyone know how to solve this? Thank you!
If I understand your explanation correctly...
Once your code has found a matrix element equal to zero it should break out of the inner for loop instead of continuing to sum. Try this:
for i=1:m^(n)
for j=1:n
if M(j,Index(i,j))~=0
K(i)= K(i)+M(j,Index(i,j))
else
K(i)=0
break
end
end
end
You should also be careful that for floating point numbers, such as the ones yielded by rand, they may not quite be equal to zero.
In the code below "G" returns a (10*4) matrix which is in the correct orientation.
All I want then is to be able to view/call these (10*4) matrices indexed by (j,k). However when I store the matrix "G" in the 4-D matrix "test" the data is displayed in a way that is a little counter intuitive? When I look at test in the variable editor I get:
val(:,:,1,1) =
1
val(:,:,2,1) =
0
val(:,:,3,1) =
0
val(:,:,4,1) =
0
.
.
.
val(:,:,1,10) =
1
val(:,:,2,10) =
0
val(:,:,3,10) =
0
val(:,:,4,10) =
0
So all the data is there but I want it displayed at a 10*4 matrix?
Also as you will see I had to change the code for "Correl_betas" and transpose "G" to get to where I am above. However I felt I did this by playing around rather than what I thought the code should be doing. Why does the original code not work? I am having to change the order of the 3rd and 4th dimensions when declaring "Correl_betas" and then pass the transpose of G, but this seems totally counter intuitive as the last two dimensions in the original "Correl_betas" and the original (un-transposed) "G" also match? But when I did it this way the ordering seemed even further from the 10*$ matrix I want.
So I have 2 questions?
1.) How can I get to the 2-dimentional (10*4) matrices I want indexed by j,k from where I am?
2.) How come the original code above doesn't result in the last two columns producing (10,4) matrices?
A large part of the problem is that I have very little experience working with matrices that have more than 2 dimensions so sorry if this question shows a lack of understanding. Maybe a pointer to a god tutorial on how to interpret manipulate higher dimensional matrices would help too.
%Correl_betas=zeros(50,50,10,4);
Correl_betas=zeros(50,50,4,10);
mats=[1:10]';
L1=-1;
for j=1:51
L1=L1+1;
L2=-1;
for k=1:51
L2=L2+1;
lambda=[ L1; L2 ];
nObs=size(mats,1);
G= [ones(nObs,1) (1-exp(-mats./lambda(1)))./(mats./lambda(1)) ((1-exp(-mats./lambda(1)))./(mats./lambda(1))-exp(-mats./lambda(1))) ((1-exp(-mats./lambda(2)))./(mats./lambda(2))-exp(-mats./lambda(2)))];
%Correl_betas(j,k,:,:)=G;
Correl_betas(j,k,:,:)=G';
test=Correl_betas(j,k,:,:);
temp1=corrcoef(Correl_betas(j,k,:,2),Correl_betas(j,k,:,3),'rows','complete');
temp2=corrcoef(Correl_betas(j,k,:,2),Correl_betas(j,k,:,4),'rows','complete');
temp3=corrcoef(Correl_betas(j,k,:,3),Correl_betas(j,k,:,4),'rows','complete');
F2_F3(j,k)=temp1(1,2);
F2_F4(j,k)=temp2(1,2);
F3_F4(j,k)=temp3(1,2);
end
end
To reshape the matrix as desired,
val2 = permute(val,[4 3 2 1]);
This brings the 4th dimension (size of 10) onto the first, and the the 3rd dimension (size of 4) onto the second.
In your loop, both j and k cycle through 1:51 so the first two dimensions of Correl_betas will end up being length 51 too.
I have a vector,"a", and a filter,"b".Both of those vectors contain only 0 or 1.
I would like to transform "a" such that any sequence of 1 only starts when b is at 1.
I have illustrated this using a loop but, as my vectors are huge, it is extremely inefficient.
The result I would like is stored in "r".
a=[0;0;1;1;1;1;1;1;0;0;1;1;0;0;1;1;1;1;1];
b=[0;0;0;0;1;0;1;0;0;1;0;1;0;1;1;0;0;0;0];
r=[0;0;0;0;1;1;1;1;0;0;0;1;0;0;1;1;1;1;1];
for i=2:length(a)
if a(i)==1 &&a(i-1)==0 && b(i)==0
a(i)=a(i-1);
end
end
assert(sum(a==r)==length(a))
Here's a two-liner:
r = a;
r([false; diff(a)>0 & b(2:end)==0]) = 0;
Please note that you need to adapt the code for row vectors (this works for column vectors).