From: Dave on
Dear fellow Matlabbers,

Is there an easier way to initialize 3d matrices?

I am currently using this code but it gives me warnings "The variable _ is changing size every ....

for i=1:N
f(1:2,1,i)=[0;0];

xf(1:2,1,i)=[13.4171;115];

a(1:2,1:2,i)=[-1 0;0 -1];
b(1:2,1:2,i)=[0 0;0 0];
c(1:2,1:2,i)=[1 0;0 1];
m(1:2,1:2,i)=[0 0;0 0];
y(1:2,1:2,i)=[0 0;0 0];
end
From: kinor on
"Dave " <Davefulton(a)rocketmail.com> wrote in message <huqe32$2l6$1(a)fred.mathworks.com>...
> Dear fellow Matlabbers,
>
> Is there an easier way to initialize 3d matrices?
>
> I am currently using this code but it gives me warnings "The variable _ is changing size every ....
>
> for i=1:N
> f(1:2,1,i)=[0;0];
>
> xf(1:2,1,i)=[13.4171;115];
>
> a(1:2,1:2,i)=[-1 0;0 -1];
> b(1:2,1:2,i)=[0 0;0 0];
> c(1:2,1:2,i)=[1 0;0 1];
> m(1:2,1:2,i)=[0 0;0 0];
> y(1:2,1:2,i)=[0 0;0 0];
> end

Hi Dave,
you have to allocate the variables first or run the loop inversely in order to get rid of the warning
so either you put
for i = N:-1:1
at the front of the loop or you allocate all variables like
put a = zeros(2,2,N)

hth
kinor
From: David Young on
"Dave " <Davefulton(a)rocketmail.com> wrote in message <huqe32$2l6$1(a)fred.mathworks.com>...
> Dear fellow Matlabbers,
>
> Is there an easier way to initialize 3d matrices?
>
> I am currently using this code but it gives me warnings "The variable _ is changing size every ....
>
> for i=1:N
> f(1:2,1,i)=[0;0];
>
> xf(1:2,1,i)=[13.4171;115];
>
> a(1:2,1:2,i)=[-1 0;0 -1];
> b(1:2,1:2,i)=[0 0;0 0];
> c(1:2,1:2,i)=[1 0;0 1];
> m(1:2,1:2,i)=[0 0;0 0];
> y(1:2,1:2,i)=[0 0;0 0];
> end

Have a look at repmat. E.g.

a = repmat([-1 0; 0 -1], [1 1 N]);

Also for some of these it's even simpler:

b = zeros(2, 2, N);
From: kinor on
"David Young" <d.s.young.notthisbit(a)sussex.ac.uk> wrote in message <huqgaq$qv0$1(a)fred.mathworks.com>...
> "Dave " <Davefulton(a)rocketmail.com> wrote in message <huqe32$2l6$1(a)fred.mathworks.com>...
> > Dear fellow Matlabbers,
> >
> > Is there an easier way to initialize 3d matrices?
> >
> > I am currently using this code but it gives me warnings "The variable _ is changing size every ....
> >
> > for i=1:N
> > f(1:2,1,i)=[0;0];
> >
> > xf(1:2,1,i)=[13.4171;115];
> >
> > a(1:2,1:2,i)=[-1 0;0 -1];
> > b(1:2,1:2,i)=[0 0;0 0];
> > c(1:2,1:2,i)=[1 0;0 1];
> > m(1:2,1:2,i)=[0 0;0 0];
> > y(1:2,1:2,i)=[0 0;0 0];
> > end
>
> Have a look at repmat. E.g.
>
> a = repmat([-1 0; 0 -1], [1 1 N]);
>
> Also for some of these it's even simpler:
>
> b = zeros(2, 2, N);

Hi David,

thanks for that I did not even have a look just adress the question

f = zeros(2,1,N);
xf = f; xf(1,1,:) = 13.4171; xf(2,1,:) = 1115;
a = zeros(2,2,N);
b = a;
a(1,1,:) = -1; a(2,2,:) = -1;
c =-a;
m = b;
y = b;

should be faster
From: Dave on
Thank you everybody x
 |  Next  |  Last
Pages: 1 2
Prev: query from textscan
Next: image read error