How to store values matrix in cell

Hello,
I have an array as follows:
t= [ 1 2 3 ];
A matrix as follows:
M = [t^2 t 1 0 0 0;
0 0 0 t^2 t 1];
Now I want to store the process the M for 3 values of t i.e in this case [1 2 3].How do I do it.
like if t=1
then M=[1^2 1 1 0 0 0;0 0 0 1^2 1 1];
if t=2
then M=[2^2 2 1 0 0 0;0 0 0 2^2 2 1]; and so on.

 Accepted Answer

Alex Mcaulley
Alex Mcaulley on 13 May 2019
Edited: Alex Mcaulley on 13 May 2019
Try this:
t = [1 2 3];
M = arrayfun(@(t) [t^2 t 1 0 0 0; 0 0 0 t^2 t 1],t,'UniformOutput',false);

3 Comments

I dont want finction is there any other way as I want to write this inside a function itself!
You can use it in your scrip as:
S = [100 0 0 0 0 0;
0 100 0 0 0 0;
0 0 100 0 0 0;
0 0 0 100 0 0;
0 0 0 0 100 0;
0 0 0 0 0 100];
prev_S = S;
Big_lambda = eye(2);
a=0;
b=0;
c=0;
p=0;
q=0;
s=0;
est_vec=[ a ; b; c; p; q; s];
theta = [ 45 46 48] ;
t= [ 1 2 3 ];
r= [200 210 220];
%Wanted = num2cell(M,1)
%[r,b,distance,angle]=deal(Wanted{:})
x = [ r; theta ; t];
%dif_x=zeros(2,3);
M = arrayfun(@(t) [t^2 t 1 0 0 0; 0 0 0 t^2 t 1],t,'UniformOutput',false);
time=4;
for i=1:3
dif_x = [(-cos(theta(i))) (r(i).*sin(theta(i))) (2*a.*t(i)+b);(-sin(theta(i))) (-r(i).*cos(theta(i))) (2*p.*t(i)+q)]
W = dif_x(i)*Big_lambda(i)*dif_x(i)'
pred_x = x+randn(3)
y = M{i} * est_vec + dif_x * (x(:,i)-pred_x(:,i))
K = prev_S*M{i}'/((W + M{i}*prev_S*M{i}'))
%S =prev_S;
est_vec_new = est_vec + K*(y-M{i}*est_vec)
cond = abs(est_vec_new - est_vec)
if cond < 0.003
break
end
est_vec = est_vec_new
a=est_vec(1,1)
b=est_vec(2,1)
c=est_vec(3,1)
p=est_vec(4,1)
q=est_vec(5,1)
s=est_vec(6,1)
S_new = (eye(6) - K*M{i})*S
S = S_new
r_cosTheta = a*time.^2+b*time+c % To calculate x co-ordinate for t>=3
r_sineTheta = p*time.^2+q*time+s % To calculate y co-ordinate for t>=3
%figure,plot(t,meas_equa1)
%new_t=[t,time];
%figure,plot(r_cosTheta,r_sineTheta)
end
%end
Thanks it is working!

Sign in to comment.

More Answers (1)

M = @(t) [t^2 t 1 0 0 0;
0 0 0 t^2 t 1];
t = [1 2 3] ;
iwant = zeros(2,6,length(t)) ;
for i = 1:length(t)
iwant(:,:,i) = M(t(i)) ;
end

5 Comments

not working says finction handle!
It will work...how you have used it? show us the full code.
S = [100 0 0 0 0 0;
0 100 0 0 0 0;
0 0 100 0 0 0;
0 0 0 100 0 0;
0 0 0 0 100 0;
0 0 0 0 0 100];
prev_S = S;
Big_lambda = eye(2);
a=0;
b=0;
c=0;
p=0;
q=0;
s=0;
est_vec=[ a ; b; c; p; q; s];
theta = [ 45 46 48] ;
t= [ 1 2 3 ];
r= [200 210 220];
%Wanted = num2cell(M,1)
%[r,b,distance,angle]=deal(Wanted{:})
x = [ r; theta ; t];
%dif_x=zeros(2,3);
M = @(t) [t^2 t 1 0 0 0;
0 0 0 t^2 t 1];
t = [1 2 3] ;
iwant = zeros(2,6,length(t)) ;
for i = 1:length(t)
iwant(:,:,i) = M(t(i)) ;
end
time=4;
for i=1:3
dif_x = [(-cos(theta(i))) (r(i).*sin(theta(i))) (2*a.*t(i)+b);(-sin(theta(i))) (-r(i).*cos(theta(i))) (2*p.*t(i)+q)]
W = dif_x(i)*Big_lambda(i)*dif_x(i)'
pred_x = x+randn(3)
y = M * est_vec + dif_x * (x(:,i)-pred_x(:,i))
K = prev_S*M'/((W + M*prev_S*M'))
%S =prev_S;
est_vec_new = est_vec + K*(y-M*est_vec)
cond = abs(est_vec_new - est_vec)
if cond < 0.003
break
end
est_vec = est_vec_new
a=est_vec(1,1)
b=est_vec(2,1)
c=est_vec(3,1)
p=est_vec(4,1)
q=est_vec(5,1)
s=est_vec(6,1)
S_new = (eye(6) - K*M)*S
S = S_new
r_cosTheta = a*time.^2+b*time+c % To calculate x co-ordinate for t>=3
r_sineTheta = p*time.^2+q*time+s % To calculate y co-ordinate for t>=3
%figure,plot(t,meas_equa1)
%new_t=[t,time];
%figure,plot(r_cosTheta,r_sineTheta)
end
%end
This is my whole code now when I am checking in workspace it says M as function handle.
error as follows:
Undefined operator '*' for input arguments of type 'function_handle'.
clc; clear all ;
S = [100 0 0 0 0 0;
0 100 0 0 0 0;
0 0 100 0 0 0;
0 0 0 100 0 0;
0 0 0 0 100 0;
0 0 0 0 0 100];
prev_S = S;
Big_lambda = eye(2);
a=0;
b=0;
c=0;
p=0;
q=0;
s=0;
est_vec=[ a ; b; c; p; q; s];
theta = [ 45 46 48] ;
t= [ 1 2 3 ];
r= [200 210 220];
%Wanted = num2cell(M,1)
%[r,b,distance,angle]=deal(Wanted{:})
x = [ r; theta ; t];
%dif_x=zeros(2,3);
M = @(t) [t^2 t 1 0 0 0;
0 0 0 t^2 t 1];
t = [1 2 3] ;
iwant = zeros(2,6,length(t)) ;
for i = 1:length(t)
iwant(:,:,i) = M(t(i)) ;
end
time=4;
for i=1:3
M = iwant(:,:,i) ;
dif_x = [(-cos(theta(i))) (r(i).*sin(theta(i))) (2*a.*t(i)+b);(-sin(theta(i))) (-r(i).*cos(theta(i))) (2*p.*t(i)+q)]
W = dif_x(i)*Big_lambda(i)*dif_x(i)'
pred_x = x+randn(3)
y = M * est_vec + dif_x * (x(:,i)-pred_x(:,i))
K = prev_S*M'/((W + M*prev_S*M'))
%S =prev_S;
est_vec_new = est_vec + K*(y-M*est_vec)
cond = abs(est_vec_new - est_vec)
if cond < 0.003
break
end
est_vec = est_vec_new
a=est_vec(1,1)
b=est_vec(2,1)
c=est_vec(3,1)
p=est_vec(4,1)
q=est_vec(5,1)
s=est_vec(6,1)
S_new = (eye(6) - K*M)*S
S = S_new
r_cosTheta = a*time.^2+b*time+c % To calculate x co-ordinate for t>=3
r_sineTheta = p*time.^2+q*time+s % To calculate y co-ordinate for t>=3
%figure,plot(t,meas_equa1)
%new_t=[t,time];
%figure,plot(r_cosTheta,r_sineTheta)
end
%end
thanks it is working!

Sign in to comment.

Categories

Products

Release

R2015b

Community Treasure Hunt

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

Start Hunting!