How can I define dynamically named matrices? - matlab

If you define a range from A1 to A10 in Excel using VBA you cam use Range("A1:A10"). On the other hand side it's possible to write down Range("A1:A"&10). How can I use the second way in MATLAB, please?
I have some matrices M1, M2, M3, ..., and I wish to define them by using iterator FOR that helps me stop writing the matrix names completely.

Notwithstanding the comment that this approach is frowned upon, below is a way to do it programmatically:
% define cell with matrix names
number_of_matrices_I_want = 5;
my_matrix_names = repmat({'NA'}, 1, number_of_matrices_I_want);
for ii = 1:length(my_matrix_names)
my_matrix_names(ii) = {strcat('M',num2str(ii))};
end
% example for how to populate a matrix from "my_matrix_names"
x = rand(5,1); % fake numbers
y = rand(5,1);
eval([my_matrix_names{1} '= [x y]']);

Related

How to run a for loop using two variables at the same time in Matlab

I am using this command in Matlab:
grazAng = grazingang(H,R)
If i fix H, I can treat R as a vector:
z=[];
for i=1:1000
z(i)=abs(grazingang(1,i));
end
Now I would like to have both H and R to by dynamic. For example:
H=[0,0.25,0.5]
R=[1,2,3]
And I would like my loop to run three times, each time selecting a pair of (H,R) values with the same indexes, i.e. (0,1),(0.25,2),(0.5,3) and then store the result in z. Could anyone help me out with this?
Remember, everything in MATLAB is an array. To do this with a loop, you need to index into the arrays:
H = [0,0.25,0.5];
R = [1,2,3];
z = zeros(size(H)); % Pre-allocation is generally advised
for i = 1:1000
z(i) = abs(grazingang(H(i),R(i)));
end
But MATLAB functions generally accept vectors and do this for you, so all you need to do is:
H=[0,0.25,0.5];
R=[1,2,3];
z = abs(grazingang(H,R));

MATLAB: How do I make 10 2D matrices into a single3D matrix using a for loop?

I have ten 2D matrices in the form
matrix_Q1 = [Q1_inner(:,1) Q1_outer(:,2) Q1_inner(:,1) Q1_outer(:,2)];
This is my current for loop, which is incorrect
n = 1;
for X_num = matrix_Q1 : matrix_Q10;
X_new(:,:,n) = X_num;
n = n+1;
end
I know how to create a 3D matrix with the x(:,:,1) syntax, but how do I do this in loop format?
Why do you want to create a 3D matrix with it? Perhaps you could just use a cell array for that instead (see here details on cells), where you can then use X_new{i} or X_new{i,j} to store each 2D matrix?
The fact that all ten of your 2D matrices are different names doesn't make it ideal for using a loop, unless you use the eval function, which I normally don't like using but in this case is necessary.
I would do this:
sz = size(matrix_Q1);
X_new = zeros(sz(1),sz(2),10);
for n = 1:10
X_new(:,:,n) = eval(['matrix_Q',num2str(n)]);
end

issue with surface with MATLAB

a = 10:100
b = 10:100
c = power(a,b)
surf(a,b,c)
=> Error using surf (line 78)
Z must be a matrix, not a scalar or vector
any clue ?
Here's a vectorized way using bsxfun:
a = 10:100;
b = 1:.1:10; %// changed b to avoid very large numbers
c = bsxfun(#power, a, b.');
surf(a,b,c)
c=power(a,b) does not give you the combinations of all a power b, unfortunately.
Here there is a way of doing it (though most likely there is a vectorizedd way of doing it)
a = 10:100;
b = linspace(1,10,length(a));
% I changed the values of b, because 100^100 is that a big number that Matlab will not plot it, it is too big for storing in a double
%loop and save
for ii=1:length(b)
c(ii,:)=a.^(b(ii));
end
surf(a,b,c)

How to generate chaotic sequences from Chen's hyperchaotic system?

I need to generate two chaotic sequences based on chen's hyperchaotic system.It has to be generated from the following four formulas
X=ay-x;
Y=-xz+dx+cy-q;
Y=xy-bz;
Q=x+k;
where a,b,c,d,x,y,z,q are all initialised as follows.
I need only X and Y
where
X=[x1,x2,...x4n]
Y=[y1,y2,...y4n]
a=36 ;
b=3 ;
c=28 ;
d=16 ;
k=0.2 ;
x=0.3 ;
y=-0.4 ;
z=1.2 ;
q=1 ;
n=256 ;
I tried the following code but i'm not able to get it properly.
clc
clear all
close all
w=imread('C:\Users\Desktop\a.png');
[m n]=size(w)
a=36;
b=3;
c=28;
d=16;
k=0.2;
x(1)=0.3;
y(1)=-0.4;
z(1)=1.2;
q(1)=1;
for i=1:1:4(n)
x(i+1)=(a*(y(i)-x(i)));
y(i+1)=-(x(i)*z(i))+(d*x(i))+(c*y(i))-q(i);
z(i+1)=(x(i)*y(i))-(b*z(i));
q(i+1)=x(i)+k;
end
disp(x);
disp(y);
pls help. thanks in advance.
Your code isn't even close to doing what you want it to. Fortunately, I'm vaguely interested in the problem and I have a bunch of spare time, so I thought I'd try and implement it step by step to show you what to do. I've left a few gaps for you to fill in.
It sounds like you want to integrate the hyperchaotic chen system, which has various definitions online, but you seem to be focusing on
So let's write a matlab function that defines that system
function vdot = chen(t, v, a, b, c, d, k)
% Here you unpack the input vector v -
x = v(1); y = v(2); z = v(3); q = v(4);
% Here you need to implement your equations as xdot, ydot etc.
% xdot = ...
% ydot = ...
% I'll leave that for you to do yourself.
% Then you pack them up into an output vector -
vdot = [xdot; ydot; zdot; qdot];
end
Save that in a file called chen.m. Now you need to define the values of the parameters a, b, c, d and k, as well as your initial condition.
% You need to define the values of a, b, c, d, k here.
% a = ...
% b = ...
% You also need to define the vector v0, which is a 4x1 vector of your
% initial conditions
% v0 = ...
%
This next line creates a function that can be used by Matlab's integration routines. The first parameter t is the current time (which you don't actually use) and the second parameter is a 4x1 vector containing x, y, z, q.
>> fun = #(t,v) chen(t,v,a,b,c,d,k)
Now you can use ode45 (which does numerical integration using a 4th order runge-kutta scheme) to integrate it and plot some paths. The first argument to ode45 is the function you want to be integrated, the second argument is the timespan to be integrated over (I chose to integrate from 0 to 100, maybe you want to do something different) and the third argument is your initial condition (which hopefully you already defined).
>> [t, v] = ode45(fun, [0 100], v0);
The outputs are t, a vector of times, and v, which will be a matrix whose columns are the different components (x, y, z, q) and whose rows are the values of the components at each point in time. So you can pull out a column for each of the x and y components, and plot them
>> x = v(:,1);
>> y = v(:,2);
>> plot(x,y)
Which gives a reasonably chaotic looking plot:
#Abirami Anbalagan and Sir #Chris Taylor, I have also studied hyperchaotic system up to some extent. According to me, for system to be chaotic, values should be like
a= 35; b= 3; c= 12; d= 7;
v(n) = [-422 -274 0 -2.4]transpose
where v(n) is a 4*1 Matrix.

Binning in matlab

I have been unable to find a function in matlab or octave to do what I want.
I have a matrix m of two columns (x and y values). I know that I can extract the column by doing m(:,1) or m(:,2). I want to split it into smaller matricies of [potentially] equal size and and plot the mean of these matricies. In other words, I want to put the values into bins based on the x values, then find means of the bins. I feel like the hist function should help me, but it doesn't seem to.
Does anyone know of a built-in function to do something like this?
edit
I had intended to mention that I looked at hist and couldn't get it to do what I wanted, but it must have slipped my mind.
Example: Let's say I have the following (I'm trying this in octave, but afaik it works in matlab):
x=1:20;
y=[1:10,10:1];
m=[x, y];
If I want 10 bins, I would like m to be split into:
m1=[1:2, 1:2]
...
m5=[9:10, 9:10]
m6=[10:11, 10:-1:9]
...
m10=[19:20, 2:-1:1]
and then get the mean of each bin.
Update: I have posted a follow-up question here. I would greatly appreciate responses.
I have answered this in video form on my blog:
http://blogs.mathworks.com/videos/2009/01/07/binning-data-in-matlab/
Here is the code:
m = rand(10,2); %Generate data
x = m(:,1); %split into x and y
y = m(:,2);
topEdge = 1; % define limits
botEdge = 0; % define limits
numBins = 2; % define number of bins
binEdges = linspace(botEdge, topEdge, numBins+1);
[h,whichBin] = histc(x, binEdges);
for i = 1:numBins
flagBinMembers = (whichBin == i);
binMembers = y(flagBinMembers);
binMean(i) = mean(binMembers);
end