How to store the output values in matrix?
5 views (last 30 days)
Show older comments
I have the following program for logistic map function,
function x=logistic(n,level,a,x0)
%Syntax: x=logistic(n,level,a,x0)
if nargin<1 | isempty(n)==1
n=64;
else
% n must be scalar
if sum(size(n))>2
error('n must be scalar.');
end
% n must be positive
if n<0
error('n must be positive.');
end
% n must be an integer
if round(n)-n~=0
error('n must be an integer.');
end
end
if nargin<2 | isempty(level)==1
level=0;
else
% level must be a scalar
if sum(size(level))>2
error('level must be scalar.');
end
% level must be positive
if level<0
error('level must be positive.');
end
end
if nargin<3 | isempty(a)==1
a=4;
else
% a must be scalar
if sum(size(a))>2
error('a must be scalar.');
end
end
if nargin<4 | isempty(x0)==1
x0=0.1;
else
% x0 must be scalar
if sum(size(x0))>2
error('x0 must be scalar.');
end
end
% Initialize
x(1,1)=a*x0*(1-x0);
% Simulate
for i=2:n
x(i,1)=a*x(i-1,1)*(1-x(i-1,1));
disp(x);
end
% Add normal white noise
x=x+randn(n,1)*level*std(x);
after running the program I get output values as
0.3600
0.9216
0.2890
0.8219
0.5854
0.9708
0.1133
0.4020
0.9616
0.1478
0.5039
0.9999
0.0002
0.0010
0.0039
0.0157
0.0617
0.2317
0.7121
0.8200
0.5904
0.9673
0.1264
0.4416
0.9864
0.0537
0.2034
0.6481
0.9122
0.3203
0.8709
0.4498
0.9899
0.0400
0.1535
0.5199
0.9984
0.0063
0.0251
0.0979
0.3533
0.9139
0.3148
0.8628
0.4736
0.9972
0.0111
0.0440
0.1684
0.5600
0.9856
0.0568
0.2144
0.6738
0.8792
0.4249
0.9775
0.0881
0.3214
0.8724
0.4453
0.9880
0.0473
0.1804
Now I want to store these values in a 8*8 matrix,how to that?
Please do help to where the required line of code should be added to the above program.
Thank you.
0 Comments
Answers (1)
madhan ravi
on 27 Apr 2019
Edited: madhan ravi
on 27 Apr 2019
Simply reshape x:
reshape(x,8,[]) % or transpose according to your orientation
Note: Preallocating a Variable is an essential step.
2 Comments
Walter Roberson
on 27 Apr 2019
x = zeros(n,1);
before you assign to x(1,1)
However, you should not be storing the values in an 8 x 8 matrix. The number of values returned in x is n, which will not necessarily be 64. If you wanted a rectangular matrix, you should factor n to determine the size to make it as a rectangle -- and keep in mind that the user might have provided an n that is a prime number.
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!