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:
Error in Array

Subject: Error in Array

From: FAISAL PEER MOAHMED

Date: 16 Aug, 2010 17:14:24

Message: 1 of 12

Hi

Please see the code below


for i= 1: 207
    
ABCD = s2abcd([S11(i) S12(i); S21(i) S22(i)], z0);
  
end


In the above code the variable ABCD does not have full size. It just stores the last value.

Can anyone help

Regards
Faisal

Subject: Error in Array

From: Andy

Date: 16 Aug, 2010 17:29:08

Message: 2 of 12

"FAISAL PEER MOAHMED" <pfaisalbe@gmail.com> wrote in message <i4brlg$k20$1@fred.mathworks.com>...
> Hi
>
> Please see the code below
>
>
> for i= 1: 207
>
> ABCD = s2abcd([S11(i) S12(i); S21(i) S22(i)], z0);
>
> end
>
>
> In the above code the variable ABCD does not have full size. It just stores the last value.
>
> Can anyone help
>
> Regards
> Faisal

Well, you're overwriting ABCD in every iteration of the for loop. What did you expect? Perhaps you meant to do the following:

ABCD = zeros(1,207); % preallocate ABCD to its final size

for ix = 1: 207 % don't use i = sqrt(-1) for your indexing variable
  ABCD(ix) = s2abcd([S11(ix) S12(ix); S21(ix) S22(ix)], z0); % fill in ABCD
end

Subject: Error in Array

From: Sean

Date: 16 Aug, 2010 17:29:08

Message: 3 of 12

"FAISAL PEER MOAHMED" <pfaisalbe@gmail.com> wrote in message <i4brlg$k20$1@fred.mathworks.com>...
> Hi
>
> Please see the code below
>
>
> for i= 1: 207
>
> ABCD = s2abcd([S11(i) S12(i); S21(i) S22(i)], z0);
>
> end
>
>
> In the above code the variable ABCD does not have full size. It just stores the last value.
>
> Can anyone help
>
> Regards
> Faisal

that's because you're redefining the variable ABCD on every loop iteration. To record every one
ABCD = zeros(207,1);
for ii = 1:207
ABCD(ii) = ...
end

Subject: Error in Array

From: FAISAL PEER MOAHMED

Date: 16 Aug, 2010 17:41:24

Message: 4 of 12

Thanks

I have tried this already , But does not work. I have tried the following

for i = 1: 10
    
    strcat('A',num2str(i)) = [i i+1 ; i i +2]
end


error is

Subscripted assignment dimension mismatch.

Please help

Faisal


"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i4bsh4$fu7$1@fred.mathworks.com>...
> "FAISAL PEER MOAHMED" <pfaisalbe@gmail.com> wrote in message <i4brlg$k20$1@fred.mathworks.com>...
> > Hi
> >
> > Please see the code below
> >
> >
> > for i= 1: 207
> >
> > ABCD = s2abcd([S11(i) S12(i); S21(i) S22(i)], z0);
> >
> > end
> >
> >
> > In the above code the variable ABCD does not have full size. It just stores the last value.
> >
> > Can anyone help
> >
> > Regards
> > Faisal
>
> that's because you're redefining the variable ABCD on every loop iteration. To record every one
> ABCD = zeros(207,1);
> for ii = 1:207
> ABCD(ii) = ...
> end

Subject: Error in Array

From: Walter Roberson

Date: 16 Aug, 2010 17:42:47

Message: 5 of 12

FAISAL PEER MOAHMED wrote:

> Please see the code below
>
>
> for i= 1: 207
> ABCD = s2abcd([S11(i) S12(i); S21(i) S22(i)], z0);
>
> end
>
>
> In the above code the variable ABCD does not have full size. It just
> stores the last value.
>
> Can anyone help

Hint: what does S11(i) mean? Why does the (i) appear there? Why doesn't the
for loop "just know" that you want that part of S11 ?

Subject: Error in Array

From: FAISAL PEER MOAHMED

Date: 16 Aug, 2010 17:45:23

Message: 6 of 12

Hi

I tried that with the following code

A =zeros(1,10)
for i = 1: 10
    
    A(i) = [i i+1 ;i i+2]
end

error is
Subscripted assignment dimension mismatch.






"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i4bsh4$fu7$1@fred.mathworks.com>...
> "FAISAL PEER MOAHMED" <pfaisalbe@gmail.com> wrote in message <i4brlg$k20$1@fred.mathworks.com>...
> > Hi
> >
> > Please see the code below
> >
> >
> > for i= 1: 207
> >
> > ABCD = s2abcd([S11(i) S12(i); S21(i) S22(i)], z0);
> >
> > end
> >
> >
> > In the above code the variable ABCD does not have full size. It just stores the last value.
> >
> > Can anyone help
> >
> > Regards
> > Faisal
>
> that's because you're redefining the variable ABCD on every loop iteration. To record every one
> ABCD = zeros(207,1);
> for ii = 1:207
> ABCD(ii) = ...
> end

Subject: Error in Array

From: FAISAL PEER MOAHMED

Date: 16 Aug, 2010 17:48:05

Message: 7 of 12

Thanks

s11(i) is the i the element of a column vector s11. Value is in complex form

Faisal



Walter Roberson <roberson@hushmail.com> wrote in message <i4btbt$dq8$1@canopus.cc.umanitoba.ca>...
> FAISAL PEER MOAHMED wrote:
>
> > Please see the code below
> >
> >
> > for i= 1: 207
> > ABCD = s2abcd([S11(i) S12(i); S21(i) S22(i)], z0);
> >
> > end
> >
> >
> > In the above code the variable ABCD does not have full size. It just
> > stores the last value.
> >
> > Can anyone help
>
> Hint: what does S11(i) mean? Why does the (i) appear there? Why doesn't the
> for loop "just know" that you want that part of S11 ?

Subject: Error in Array

From: Andy

Date: 16 Aug, 2010 18:00:21

Message: 8 of 12

> for i = 1: 10
>
> A(i) = [i i+1 ;i i+2]
> end
>
> error is
> Subscripted assignment dimension mismatch.

Well of course. You are trying to assign a 2x2 matrix to one spot in an array, which makes no sense.

Subject: Error in Array

From: Walter Roberson

Date: 16 Aug, 2010 18:02:44

Message: 9 of 12

Sean wrote:

> that's because you're redefining the variable ABCD on every loop
> iteration. To record every one
> ABCD = zeros(207,1);
> for ii = 1:207
> ABCD(ii) = ...
> end

To solve the problem of storing multiple values:

ABCD = cell(207,1);
for ii = 1:207
   ABCD{ii} = .....
end


This does not assume that the same number of values will be stored for each
iteration.

Subject: Error in Array

From: FAISAL PEER MOAHMED

Date: 16 Aug, 2010 18:21:04

Message: 10 of 12

Walter

Thanks. Yes I need to store a 2*2 matrix in a variable inside the for loop.

Can it be done ?

Regards

Faisal


Walter Roberson <roberson@hushmail.com> wrote in message <i4buh9$f28$2@canopus.cc.umanitoba.ca>...
> Sean wrote:
>
> > that's because you're redefining the variable ABCD on every loop
> > iteration. To record every one
> > ABCD = zeros(207,1);
> > for ii = 1:207
> > ABCD(ii) = ...
> > end
>
> To solve the problem of storing multiple values:
>
> ABCD = cell(207,1);
> for ii = 1:207
> ABCD{ii} = .....
> end
>
>
> This does not assume that the same number of values will be stored for each
> iteration.

Subject: Error in Array

From: Andy

Date: 16 Aug, 2010 18:30:27

Message: 11 of 12

Faisal, Walter has shown you how to do this:

ABCD = cell(207,1);
for ii = 1:207
   ABCD{ii} = .....
end

When you fill in the "...." with the appropriate data and run the code. You will have a 207x1 cell array, each cell containing your 2x2 data. If you want to turn the cell array into a numeric array later, read the documentation for cell2mat.

Subject: Error in Array

From: Walter Roberson

Date: 16 Aug, 2010 18:34:47

Message: 12 of 12

FAISAL PEER MOAHMED wrote:

> Thanks. Yes I need to store a 2*2 matrix in a variable inside the for
> loop.
> Can it be done ?

ABCD = zeros(2,2,207);
for ii = 1:207
   ABCD(:,:,ii) = ....
end

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