Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Add Cell to matrix

Subject: Add Cell to matrix

From: roya olyazadeh

Date: 7 May, 2010 19:56:04

Message: 1 of 8

I have this matrix :
X = [12 23 24 32 22]
I want to add cell from another matrix to it but I don't want any cell from this matrix will be removed.
for example I want add X0(i) to it. i=4
X0(4)= 42

Now I want to have : X =[12 23 24 42 32 22 ]
One shift in my matrix.
tnx

Subject: Add Cell to matrix

From: us

Date: 7 May, 2010 20:08:04

Message: 2 of 8

"roya olyazadeh" <roya2543@gmail.com> wrote in message <hs1r8k$g33$1@fred.mathworks.com>...
> I have this matrix :
> X = [12 23 24 32 22]
> I want to add cell from another matrix to it but I don't want any cell from this matrix will be removed.
> for example I want add X0(i) to it. i=4
> X0(4)= 42
>
> Now I want to have : X =[12 23 24 42 32 22 ]
> One shift in my matrix.
> tnx

one of the many solutions

     x=[1,2,3];
     ix=3;
     xv=nan;
     x=[x(1:ix-1),xv,x(ix:end)]
% x = 1 2 NaN 3

us

Subject: Add Cell to matrix

From: roya olyazadeh

Date: 7 May, 2010 20:24:06

Message: 3 of 8

"us " <us@neurol.unizh.ch> wrote in message <hs1rv4$2lc$1@fred.mathworks.com>...
> "roya olyazadeh" <roya2543@gmail.com> wrote in message <hs1r8k$g33$1@fred.mathworks.com>...
> > I have this matrix :
> > X = [12 23 24 32 22]
> > I want to add cell from another matrix to it but I don't want any cell from this matrix will be removed.
> > for example I want add X0(i) to it. i=4
> > X0(4)= 42
> >
> > Now I want to have : X =[12 23 24 42 32 22 ]
> > One shift in my matrix.
> > tnx
>
> one of the many solutions
>
> x=[1,2,3];
> ix=3;
> xv=nan;
> x=[x(1:ix-1),xv,x(ix:end)]
> % x = 1 2 NaN 3
>
> Dear us
It can't help maybe I want to add X0(1)= 11 and also X0(4)=42 to X
and then I have X=[11 12 23 24 42 32 22]
Adding to this matrix it depends on something and it can be changeable. maybe one time 6 items will be added to this matrix.

Subject: Add Cell to matrix

From: us

Date: 7 May, 2010 20:41:05

Message: 4 of 8

"roya olyazadeh" <roya2543@gmail.com> wrote in message <hs1st6$39v$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hs1rv4$2lc$1@fred.mathworks.com>...
> > "roya olyazadeh" <roya2543@gmail.com> wrote in message <hs1r8k$g33$1@fred.mathworks.com>...
> > > I have this matrix :
> > > X = [12 23 24 32 22]
> > > I want to add cell from another matrix to it but I don't want any cell from this matrix will be removed.
> > > for example I want add X0(i) to it. i=4
> > > X0(4)= 42
> > >
> > > Now I want to have : X =[12 23 24 42 32 22 ]
> > > One shift in my matrix.
> > > tnx
> >
> > one of the many solutions
> >
> > x=[1,2,3];
> > ix=3;
> > xv=nan;
> > x=[x(1:ix-1),xv,x(ix:end)]
> > % x = 1 2 NaN 3
> >
> > Dear us
> It can't help maybe I want to add X0(1)= 11 and also X0(4)=42 to X
> and then I have X=[11 12 23 24 42 32 22]
> Adding to this matrix it depends on something and it can be changeable. maybe one time 6 items will be added to this matrix.

well... so what(?)...
the engine is the same - did you even look at it(?)...

us

Subject: Add Cell to matrix

From: roya olyazadeh

Date: 7 May, 2010 21:07:06

Message: 5 of 8

"us " <us@neurol.unizh.ch> wrote in message <hs1tt1$7li$1@fred.mathworks.com>...
> "roya olyazadeh" <roya2543@gmail.com> wrote in message <hs1st6$39v$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <hs1rv4$2lc$1@fred.mathworks.com>...
> > > "roya olyazadeh" <roya2543@gmail.com> wrote in message <hs1r8k$g33$1@fred.mathworks.com>...
> > > > I have this matrix :
> > > > X = [12 23 24 32 22]
> > > > I want to add cell from another matrix to it but I don't want any cell from this matrix will be removed.
> > > > for example I want add X0(i) to it. i=4
> > > > X0(4)= 42
> > > >
> > > > Now I want to have : X =[12 23 24 42 32 22 ]
> > > > One shift in my matrix.
> > > > tnx
> > >
> > > one of the many solutions
> > >
> > > x=[1,2,3];
> > > ix=3;
> > > xv=nan;
> > > x=[x(1:ix-1),xv,x(ix:end)]
> > > % x = 1 2 NaN 3
> > >
> > > Dear us
> > It can't help maybe I want to add X0(1)= 11 and also X0(4)=42 to X
> > and then I have X=[11 12 23 24 42 32 22]
> > Adding to this matrix it depends on something and it can be changeable. maybe one time 6 items will be added to this matrix.
>
> well... so what(?)...
> the engine is the same - did you even look at it(?)...
>
> us

Yes I looked at it and then replied to you. I cant add it manually. As I told
I have X0 matrix with N row .

for i = 1 : length(stn)
    if fix1(i)==1
        X=[X(1:i),X0(i),X(i+1:end)]
        
    end
end

error:
All matrices on a row in the bracketed expression must have the
 same number of rows.

Subject: Add Cell to matrix

From: us

Date: 8 May, 2010 00:08:03

Message: 6 of 8

"roya olyazadeh"
a) > I have X0 matrix with N row .
b) > X=[X(1:i),X0(i),X(i+1:end)]
c) > error:
   > All matrices on a row in the bracketed expression must have the
   > same number of rows.

well...
1) at the command prompt, type
     dbstop if error;
2) run your code
3) the error occurs
4) open the file at the offending line
5) examine these
     i
     X(1:i)
     X0(i)
     X(i+1:end)
6) see why things go bad
7) then
     dbquit
     dbclear all;

us

Subject: Add Cell to matrix

From: roya olyazadeh

Date: 8 May, 2010 06:01:20

Message: 7 of 8

"us " <us@neurol.unizh.ch> wrote in message <hs2a13$69s$1@fred.mathworks.com>...
> "roya olyazadeh"
> a) > I have X0 matrix with N row .
> b) > X=[X(1:i),X0(i),X(i+1:end)]
> c) > error:
> > All matrices on a row in the bracketed expression must have the
> > same number of rows.
>
> well...
> 1) at the command prompt, type
> dbstop if error;
> 2) run your code
> 3) the error occurs
> 4) open the file at the offending line
> 5) examine these
> i
> X(1:i)
> X0(i)
> X(i+1:end)
> 6) see why things go bad
> 7) then
> dbquit
> dbclear all;
>
> us


tnx .It works
My mistake was : I should write X=X' ;
then

 
for i = 1 : length(stn) % number of station
    if fix1(i)==1 % If station is fixed
        if fix1(i)==fix(i+1)
           X=[X(1:i-1),X0(i),X0(i+1),X(i:end)];
       else
             X=[X(1:i-1),X0(i),X(i:end)];
        end
    end
end

Subject: Add Cell to matrix

From: Jan Simon

Date: 8 May, 2010 16:54:06

Message: 8 of 8

Dear Roya!

> for i = 1 : length(stn) % number of station
> if fix1(i)==1 % If station is fixed
> if fix1(i)==fix(i+1)
> X=[X(1:i-1),X0(i),X0(i+1),X(i:end)];
> else
> X=[X(1:i-1),X0(i),X(i:end)];
> end
> end
> end

Your problem seems to be solved - fine.

The example code, you've shown here, contains the symbols "fix1" and "fix" without any explanations. If "fix" is not Matlab's builtin function FIX, then be aware that overloading function names usually leads to unexpected errors, because the function and the variable can be easily confused later on.

Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us