Saving output of for loop after each iteration (for a function)

y = 0:0.05:1;
u= 2*y-(2*y.^3)+y.^4;
a = 0:0.01:1;
b = 4000;
Carray=zeros(1,101)
for i=1:size(a)
c=A_function(y,u,b,a(i));
Carray(i)=c;
end
I want to run the function 'c' for all values of 'a'
and I want to store value of 'c' for each 'a'
The error i get is "Unable to perform assignment because the left and right sides have a different number of elements"
Can someone please let me know what I am doing wrong?
Thanks

Answers (2)

Here is the corrected code of yours:
y = 0:0.05:1;
u= 2*y-(2*y.^3)+y.^4;
a = 0:0.01:1;
b = 4000;
C=zeros(numel(a),numel(u));
for ii=1:size(a)
C(ii,:)=A_function(y,u,b,a(ii));
end

3 Comments

Now I get the error “unable to perform assignment because the size of the left side is 1-by-101 and the size of the right side is 100-by-1”
I tried C(ii-1,:) = A_function(y,u,b,a(ii)); But that was an invalid input.
Also I think that the array size row vs Columns is creating a problem left to right?
Thanks
I fixed the error by changing this line “For ii=1:size(a)-1”
But now my C matrix is still all 0’s
%{
C=zeros(numel(a),numel(u));
for ii=1:size(a)
%}
I agree with the first line but not the second. Before looking at the output of the code below, how many lines of text does it display?
a = 0:0.1:1;
for k = 1:size(a)
fprintf("k is %d\n", k)
end
k is 1
Now compare this with using numel instead of size.
a = 0:0.1:1;
for k = 1:numel(a)
fprintf("k is %d\n", k)
end
k is 1 k is 2 k is 3 k is 4 k is 5 k is 6 k is 7 k is 8 k is 9 k is 10 k is 11
Why the difference in behavior? When any of the inputs to the colon function or : operator is non-scalar and non-empty, MATLAB will just use the first element of that input.
sz = size(a)
sz = 1×2
1 11
v = 1:sz % equivalent to 1:sz(1)
v = 1
While your code would have worked for a column vector, the numel approach works in general to allow you to loop through all the elements in an array.

Sign in to comment.

Try this:
y = 0:0.05:1;
u = 2*y-(2*y.^3)+y.^4;
a = 0:0.01:1;
b = 4000;
rows = length(a)
columns = 101;
Carray = zeros(rows, columns);
for row = 1 : rows
resultVector = A_function(y, u, b, a(row));
Carray(row, :) = resultVector;
end
function c = A_function(y, u, b, aValue)
c = randi(9, 1, 101); % Whatever it may be...
end
Obviously replace the A_function with your actual function that returns a vector. Carray will not be all zeros.

8 Comments

Thanks Image Analyst. I get the error “Unable to perform assignment because of the size of the left side is 1-by-101 and the size of the right side is 101-by-101”
This error is for line Carray(row, :) = resultVector;
I changed it to Carray(row, columns) = resultVector; But that made it a 1-by-1
Also what is ‘aValue’ in function c?
Thanks
It appears that the error is in the Carray(row, :) = resultVector; Line
Because before I run that line row=101 But after I run it, row =1
My code runs fine with no errors. If you have errors it's because you replaced the A_function with the actual function, and you did not supply that to us. Please attach it.
When you call A_function you pass it a(i) which is a single value, not a vector. That is fine, however, you cannot define the function like this:
function c = A_function(y, u, b, a(i))
because it will throw an error. The 4th argument needs to be a simple variable name that represents a(i) and I called it aValue.
function c=OS_Eigen2D(y,u,b,a,N)
% Set up as a function, the following input parameters are required.
% INPUT:
% y
% u
% b
% a
% N = (Optional)
Discretization order for Chebyshev polynomials
% N is defaulted to be N= 101 if otherwise specified; No need to
% change in general
% OUTPUT
% c
% N is the number of data points for the Gauss-Lobato interpolation (optional)
if ~exist('N','var')
% N parameter does not exist, so default it to something
N = 101;
end
%%SET-UP DIFFERENTIATION MATRICES
[D,yGL]=cheb(N);
D2=D^2;
D2=D2(2:N,2:N);
S=diag([0; 1./(1-yGL(2:N).^2); 0]);
D4=(diag(1-yGL.^2)*D^4-8*diag(yGL)*D^3-12*D^2)*S;
D4=D4(2:N,2:N);
D0=eye(N-1);
%%INTERPOLATE TO GAUSS-LOBATTO POINTS AND TRANSFORM COORDINATES
ymax=max(y);
y_trnsfrm=2.*y./ymax-1;
dzdy=2/ymax;
uGL=interp1(y_trnsfrm,u,yGL,'spline');
dduGL=(dzdy^2)*(D^2)*uGL;
uGL=uGL(2:end-1);
dduGL=dduGL(2:end-1);
D2=(dzdy^2)*D2;
D4=(dzdy^4)*D4;
%%DISCRETIZE O-S EQUATION
ak2=a^2;
R0=D2-ak2*D0;
R1=(1i/b/a)*(D4-2*ak2*D2+(ak2^2)*D0);
R1=R1+(uGL*ones(1,length(uGL))).*R0-(dduGL*ones(1,length(uGL))).*D0;
%%SOLVE
c=eig(R1,R0);
end
%%Chebyshev compute D = differentiation matrix, x = Chebyshev grid
function [D,x] = cheb(N)
if N==0, D=0; x=1; return, end
x = cos(pi*(0:N)/N)';
c = [2; ones(N-1,1); 2].*(-1).^(0:N)';
X = repmat(x,1,N+1);
dX = X-X';
D = (c*(1./c)')./(dX+(eye(N+1))); % off-diagonal entries
D = D - diag(sum(D')); % diagonal entries
end
Unrecognized function or variable 'alp'.
Error in test7>OS_Eigen2D (line 74)
ak2=alp^2;
Error in test7 (line 20)
resultVector = OS_Eigen2D(y, u, b, a(row));
What is alp?
Sorry alp should be ‘a’
Forgot to change that before posting. I will update original function code as well
Hi Image Analyst,
Did that work for you? I have to turn in my work tonight so just wondering if you were able to troubleshoot why it’s not working with the actual function.
Thanks
Sorry, I didn't get a chance last night, but I guess it's past the homework deadline so it doesn't matter at this point.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

JD
on 1 Apr 2021

Commented:

on 2 Apr 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!