How to access array element sequentially in for loop in MATLAB? - arrays

I have 5 dataset:
data1=rand(1,1000)
data2=rand(1,1000)
data3=rand(1,1000)
data4=rand(1,1000)
data5=rand(1,1000)
I have to select the values sequentially and a non-repetitive way.
My whole pocess is for i=1,
Result1(index1) = data1(index1)+data1(2)
Result2(index1) = data2(index1)+data2(index2)
result3(index1) = data1(index3)+data2(index1)+data3(index1)
result4(index1) = data1(index4)+data3(index2)
And i want to continue this process for i=200,where array index should be in sequential way.
like following way:
Result1(index2) = data1(index5)+data1(6)
Result2(index2) = data2(index3)+data2(index4)
result3(index2) = data1(index7)+data2(index3)+data3(index3)
result4(index2) = data1(index8)+data3(index4)
Thank you for your time and considerations.

METHOD 1: using a FOR loop (SLOW)
data1=rand(1,1000);
data2=rand(1,1000);
data3=rand(1,1000);
data4=rand(1,1000); % ! not used
data5=rand(1,1000); % ! not used
% Preallocation of memory for better performance
result1 = zeros(1,200);
result2 = zeros(1,200);
result3 = zeros(1,200);
result4 = zeros(1,200);
% Main loop
for i=1:200
i1 = (i-1)*4+1; % takes values 1, 5, 9, 13,...
i2 = (i-1)*4+2; % takes values 2, 6, 10, 14,...
i3 = (i-1)*4+3; % takes values 3, 7, 11, 15,...
i4 = (i-1)*4+4; % takes values 4, 8, 12, 16,...
result1(i) = data1(i1)+data1(i2);
result2(i) = data2(i1)+data2(i2);
result3(i) = data1(i3)+data2(i1)+data3(i1);
result4(i) = data1(i4)+data3(i2);
end
METHOD 2: using MATRIX indexing operations (FAST)
imat = 1:200;
i1mat = 4*(imat-1)+1;
i2mat = 4*(imat-1)+2;
i3mat = 4*(imat-1)+3;
i4mat = 4*(imat-1)+4;
result1 = data1(i1mat)+data1(i2mat);
result2 = data2(i1mat)+data2(i2mat);
result3 = data1(i3mat)+data2(i1mat)+data3(i1mat);
result4 = data1(i4mat)+data3(i2mat);
You can also avoid to construct imat,i1mat,i2mat,i3mat,i4mat and just do:
result1 = data1(1:4:800)+data1(2:4:800);
result2 = data2(1:4:800)+data2(2:4:800);
result3 = data1(3:4:800)+data2(1:4:800)+data3(1:4:800);
result4 = data1(4:4:800)+data3(2:4:800);
All the methods give the same results.

Related

Why does this code has an error of incompatible array size?

*[I found that the error occurred at line 57, z2 = w12.a1 + b12; with incompatible array size,
May I know how to fix it? Basically the data is from the simulation on MATLAB Simulink, so the size of the array changes as we increase the inputs from the original code which only have 1 input type][1]
%% load training data (use training_data.mat attached file)
% initialize number of nodes, weights, biases, errors and gradients.
%epochs and mini-batch size
clc
data = [out.dutycycle out.voltage out.current]
sample=1000;
labels = data(1:sample,1);
y = labels'; %output vector
images = data(1:sample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;
images = images'; %Input vectors
hn1 = 80; %Number of neurons in the first hidden layer
hn2 = 60; %Number of neurons in the second hidden layer
%Initializing weights and biases
w12 = randn(hn1,1000).*sqrt(2/1000);
w23 = randn(hn2,hn1)*sqrt(2/hn1);
w34 = randn(1,hn2)*sqrt(2/hn2);
b12 = randn(hn1,1);
b23 = randn(hn2,1);
b34 = randn(1,1);
%learning rate
eta = 0.0058;
%Initializing errors and gradients
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
epochs = 50;
m = 10; %Minibatch size
%% Training phase
for k = 1:epochs %Outer epoch loop
batches = 1;
for j = 1:sample/m
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
for i = batches:batches+m-1 %Loop over each minibatch
%Feed forward
a1 = images(:,i);
z2 = w12.*a1 + b12;
a2 = elu(z2);
z3 = w23*a2 + b23;
a3 = elu(z3);
z4 = w34*a3 + b34;
a4 = elu(z4); %Output vector
%backpropagation
error4 = (a4-y(:,i)).*elup(z4);
error3 = (w34'*error4).*elup(z3);
error2 = (w23'*error3).*elup(z2);
errortot4 = errortot4 + error4;
errortot3 = errortot3 + error3;
errortot2 = errortot2 + error2;
grad4 = grad4 + error4*a3';
grad3 = grad3 + error3*a2';
grad2 = grad2 + error2*a1';
end
%Gradient descent
w34 = w34 - eta/m*grad4;
w23 = w23 - eta/m*grad3;
w12 = w12 - eta/m*grad2;
b34 = b34 - eta/m*errortot4;
b23 = b23 - eta/m*errortot3;
b12 = b12 - eta/m*errortot2;
batches = batches + m;
end
fprintf('Epochs:');
disp(k) %Track number of epochs
[images,y] = shuffle(images,y); %Shuffles order of the images for next epoch
end
disp('Training done!')
%note : create folder for this experiment and save the parameters.
% don't forget to keep the folder in matlab path!
save('wfour.mat','w34');
save('wthree.mat','w23');
save('wtwo.mat','w12');
save('bfour.mat','b34');
save('bthree.mat','b23');
save('btwo.mat','b12');
%% Testing phase
% load testing data with labels ... (use testing_data.mat attached file)
testsample = 100;
labels = test(1:testsample,1);
y = labels';
images = test(1:testsample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;%copy
images = images';
we34 = matfile('wfour.mat');
w4 = we34.w34;
we23 = matfile('wthree.mat');
w3 = we23.w23;
we12 = matfile('wtwo.mat');
w2 = we12.w12;
bi34 = matfile('bfour.mat');
b4 = bi34.b34;
bi23 = matfile('bthree.mat');
b3 = bi23.b23;
bi12 = matfile('btwo.mat');
b2 = bi12.b12;
success = 0;
n = testsample;
for i = 1:n
out2 = elu(w2*images(:,i)+b2);
out3 = elu(w3*out2+b3);
out = elu(w4*out3+b4);
big = 0;
num = 0;
for k = 1:10
if out(k) > big
num = k-1;
big = out(k);
end
end
if labels(i) == num
success = success + 1;
end
end
fprintf('Accuracy: ');
fprintf('%f',success/n*100);
disp(' %');
%% ELU activation function
function fr = elu(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = x(i);
else
f(i) = 0.2*(exp(x(i))-1);
end
end
fr = f;
end
%% derivative of the ELU activation function
function fr = elup(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = 1;
else
f(i) = 0.2*exp(x(i));
end
end
fr = f;
end
%% SHuffle function
function [B,v] = shuffle(A,y)
cols = size(A,2);
P = randperm(cols);
B = A(:,P);
v = y(:,P);
end
Your help is much appreciated, thank you
MATLAB Error :
Arrays have incompatible sizes for this operation.
Error in ANN_PV_Array (line 57)
z2 = w12.*a1 + b12;

Storing numpy.ndarrays from a loop

I am trying to store the numpy.ndarrays defined as x_c, y_c, and z_c for every iteration of the loop:
for z_value in np.arange(0, 5, 1):
ms.set_current_mesh(0)
planeoffset : float = z_value
ms.compute_planar_section(planeaxis = 'Z Axis', planeoffset = planeoffset)
m = ms.current_mesh()
matrix_name = m.vertex_matrix()
x_c = matrix_name[:,0]
y_c = matrix_name[:,1]
z_c = matrix_name[:,2]
I would like to be able to recall the three arrays at any z_value, preferably with reference to the z_value i.e x_c # z_value = 2 or similar.
Thanks for any help!
p.s very new to coding, so please go easy on me.
You have to store each array in an external variable, for example a dictionary
x_c={}
y_c={}
z_c={}
for z_value in np.arange(0, 5, 1):
ms.set_current_mesh(0)
planeoffset = float(z_value)
ms.compute_planar_section(planeaxis = 'Z Axis', planeoffset = planeoffset)
m = ms.current_mesh()
m.compact()
print(m.vertex_number(), "vertices in Planar Section Z =", planeoffset)
matrix_name = m.vertex_matrix()
x_c[planeoffset] = matrix_name[:,0]
y_c[planeoffset] = matrix_name[:,1]
z_c[planeoffset] = matrix_name[:,2]
Please, ensure you call m.compact() before accessing the vertex_matrix or you will get a MissingCompactnessException error. Please, note that it is not the same to store anything in x_c[2] or in x_c[2.0], so choose if your index has to be integers o floats and keep the same type (in this example, they are floats).
Later, you can recall values like this:
print("X Values with z=2.0")
print(x_c[2.0])

Using two array's in one application with progressing the arrays

I have the below code I'm trying to put together and I'm running into a Run-time error '9' subscript out of range. This does work through the first run through then errors. I don't see why it won't allow for the string to go forward. From what I'm reading it should go through the application changing the X values with Y value 1 and when completed with that set to go to the next Y and start the whole process again until the end of Y. Any help would be appreciated.
Dim Cat(1 To 10) As String
Cat(1) = "010" 'SD
Cat(2) = "020" 'FD
Cat(3) = "050" 'WVID
Cat(4) = "040" 'VID
Cat(5) = "030" 'MEM
Cat(6) = "080" 'ACC
Cat(7) = "060" 'HDMI
Cat(8) = "070" 'SSD
Cat(9) = "090" 'POWER
Cat(10) = "990" 'ZRM
Dim Month(1 To 12) As String
Month(1) = "January"
Month(2) = "February"
Month(3) = "March"
Month(4) = "April"
Month(5) = "May"
Month(6) = "June"
Month(7) = "July"
Month(8) = "August"
Month(9) = "September"
Month(10) = "October"
Month(11) = "November"
Month(12) = "December"
For Y = 1 To UBound(Cat)
For X = 1 To UBound(Month)
Month(X) = Application.WorksheetFunction.SumIf(Sheets(Month(X)).Columns("AO"), Cat(Y), Sheets(Month(X)).Columns("AG"))
Next X
Cells(3 + Y, 41).Value = Application.WorksheetFunction.Sum(Month(1), Month(2), Month(3), Month(4), Month(5), Month(6), Month(7), Month(8), Month(9), Month(10), Month(11), Month(12))
Next Y
End Sub
On the first run through the loop indexed by X you are computing a conditional sum of data stored in Sheet "January". And then overwriting "January" with that value in Month(X). Let's say that that value is 42. On your next run through the loop 42 is in Month(X) so you are looking for Sheets(42), which probably isn't a valid worksheet. I would try this instead.
dim temp as double
For Y = 1 To UBound(Cat)
temp = 0# '0 as a double.
For X = 1 To UBound(Month)
temp = temp + Application.WorksheetFunction.SumIf(Sheets(Month(X)).Columns("AO"), Cat(Y), Sheets(Month(X)).Columns("AG"))
Next X
Cells(3 + Y, 41).Value = temp
Next Y
This way we don't need to store all of the sums from each sheet, since we only use them to add them all together.

Matlab : How to highlight GLYCINE residues in my Ramachandran plot? [duplicate]

This question already has an answer here:
MATLAB : What is the mistake in my Ramachandran plot?
(1 answer)
Closed 8 years ago.
I am trying matlab to plot ramachandran plot, without using built in command. I have succeeded too. Now I wanted to spot the GLYCINEs alone in the scatter array. Any ideas how to do this? (link to 1UBQ.pdb file : http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=1UBQ)
% Program to plot Ramanchandran plot of Ubiquitin
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
scatter(phi,psi)
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
The output is :
EDIT : Is my plot correct? Biopython: How to avoid particular amino acid sequences from a protein so as to plot Ramachandran plot? has an answer which has slightly different plot.
The modified code is as below :
% Program to plot Ramanchandran plot of Ubiquitin with no glycines
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
scatter(angle(1,:),angle(2,:))
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
which gives output (with no GLY) as below :
I would change this code block to use logical indexing
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
Instead:
residues = strsplit(p.Sequency.ResidueNames,' ');
glycine = ismember(residues,'GLY');
angle = [phi;psi];
angleNoGLY= angle(:,~glycine);
Doing it this way, if you wanted to highlight glycine (or any other residue) you can easily call it out:
angleGLY = angle(:,glycine);
plot(angleNoGLY(1,:),angleNoGLY(2,:),'ob')
line(angleGLY(1,:),angleGLY(2,:),'Marker','o','Color','r','LineStyle','none')

Matlab- how can I change an array since it is seen as double

I need to iterate Newton-Raphson.The problem is:
For mmm=1:
1) If m=1 take c1=c1b and c2=1-c1 and do the loop for u1,2(i) and p1,2(i)
2)If m=2 take c1=c1+dc and c2=1-c1, and this time do the loop with new c1 and c2 for u1,2(i) and p1,2(i)
3) If m=3 take c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2)) and do the loop for new c1 and c2.
Then increase the iteration number: mmm=2 ;
mmm keeps count of the number of N-R iterations. The first iteration has mmm=1, the second mmm=2, etc. (This particular run only do 2 iterations).
sumint are inside of the integrals. 'c1, c2 are the camber effects, u1 u2 are the velocities, p1 p2 are pressures.
Relevant part of the code:
ii=101;
ub = cell(2, 1);
ini_cond = [0,0];
for i = 1:2;
ub{i} = zeros(1,ii);
ub{i}(:, ii) = ini_cond(i) * rand(1, 1);
end
for i=1:ii;
x=i*dx;
fikness = fik*sin(pi.*x);
ub{1}(i) = (c1b-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
ub{2}(i) = (c2b+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5));
end
mmm = 1;
c1 = c1b;
m = 1;
c2=1-c1;
u = cell(2, 1);
ini_cond = [0,0];
for i = 1:2;
u{i} = zeros(1,ii);
u{i}(:, ii) = ini_cond(i) * rand(1, 1);
end
for i=1:ii;
x=(i-1)*dx;
fikness = fik*sin(pi.*x);
u{1}(i) = (c1-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
u{2}(i)= (c2+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5));
end
p = cell(2, 1);
q = cell(2, 1);
for i = 1:2;
p{i} = zeros(1,100);
q{i} = zeros(1,100);
end
p{1}(1) = 0.5*(1.-u{1}(1).^2);
q{1}(1) = 0;
p{2}(1) = 0.5*(1.-u{2}(1).^2);
q{2}(1) = 0;
for i = 2:ii;
q{1}(i) = q{1}(i-1)-dx*(u{1}(i-1)-ub{1}(i-1))./dt;
p{1}(i) = 0.5*(1.-u{1}(i).^2)+q{1}(i);
q{2}(i) = q{2}(i-1)-dx*(u{2}(i-1)-ub{2}(i-1))./dt;
p{2}(i) = 0.5*(1.-u{2}(i).^2)+q{2}(i);
end
st = zeros(2, 1);
st(1,:) = p{1}(100)-p{2}(100);
m = m+1;
if m==3;
c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2));
c2=1-c1;
end
sumint = cell(2, 1);
for i = 1:2
sumint{i} = zeros(1,length(x));
end
sumint{1}(1) = 0.5*(p{2}(1)-p{1}(1));
sumint{2}(1) = 0.5*(p{2}(1)-p{1}(1)).*(-1/2);
for i = 2:100;
x=(i-1)*dx;
sumint{1}(i) = sumint{1}(i-1)+(p{2}(i)-p{1}(i));
sumint{2}(i) = sumint{2}(i-1)+(p{2}(i)-p{1}(i)).*(x-1/2);
end
The error is: ??? Attempted to access u.%cell(2); index out of bounds because numel(u.%cell)=1.
Error in ==> grab3_SmithWilson at 75 p{1}(i)=0.5*(1.-u{1}(i).^2)+q{1}(i);
You need to show us what you want to see. When I run the code you posted now, I find that first ub is printed as you have written, then each cell of ub is overwritten on each loop iteration. What I mean is that you are not putting values into the arrays that are stored in the cells, you are putting values in to the cells themselves. Are you sure that's what you want?
If you want to store the calculation in the elements of the arrays that are stored in the cells, the following will work:
for i=1:ii;
x=(i-1)*dx;
fikness=fik*sin(pi.*x);
ub{1}(i)=(c1b-H1D*(x-0.5)+AD/2*(x-0.5)^2)/(H1-0.5*fikness-A*(x-0.5));
ub{2}(i)=(c2b+H1D*(x-0.5)-AD/2*(x-0.5)^2)/(1-H1+0.5*fikness+A*(x-0.5));
end
>> ub
ub =
[1x101 double]
[1x101 double]
This is why I suggested reading about accessing parts of cells. Really though, this is just a guess until you tell us what you want from your script.

Resources