COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Creating a matrix using a for loop

• Email
• Follow

```hi,
I'm trying to create a matrix of 8X12 by the code below but I get a matrix of 12X12.
does anyone understands where is my mistake?

genes_val=zeros(8,12)
val_ind=1
for j=1:12;
for i=1:8;
genes_vals(i,j)=mean_qty(val_ind)
val_ind=val_ind+1
end
end

Thanks!
```
 0
Reply pixel 1/28/2011 7:45:03 AM

See related articles to this posting

```"pixel" wrote in message <ihts5v\$8vq\$1@fred.mathworks.com>...
> hi,
> I'm trying to create a matrix of 8X12 by the code below but I get a matrix of 12X12.
> does anyone understands where is my mistake?
>
> genes_val=zeros(8,12)
>     val_ind=1
> for j=1:12;
>    for i=1:8;
>         genes_vals(i,j)=mean_qty(val_ind)
>         val_ind=val_ind+1
>     end
> end
>
> Thanks!

it's working just fine
genes_vals is 8X12

you also don't need the ; in the for line, for j=1:12; is the same as for j=1:12
```
 0
Reply Paulo 1/28/2011 8:03:04 AM

```"Paulo Silva" wrote in message <ihtt7o\$85t\$1@fred.mathworks.com>...
> "pixel" wrote in message <ihts5v\$8vq\$1@fred.mathworks.com>...
> > hi,
> > I'm trying to create a matrix of 8X12 by the code below but I get a matrix of 12X12.
> > does anyone understands where is my mistake?
> >
> > genes_val=zeros(8,12)
> >     val_ind=1
> > for j=1:12;
> >    for i=1:8;
> >         genes_vals(i,j)=mean_qty(val_ind)
> >         val_ind=val_ind+1
> >     end
> > end
> >
> > Thanks!
>
> it's working just fine
> genes_vals is 8X12
>
> you also don't need the ; in the for line, for j=1:12; is the same as for j=1:12

Instead you might want to add ; in the end of all other lines.
I found no mistakes in the code, assuming you really want 8 x 12 matrix.
More "MATLAB-type" code producing the same result could be:
genes_val=zeros(8,12);
genes_val(:) = mean_qty;
or
genes_val=reshape(mean_qty,8,12);
```
 0
Reply Pekka 1/28/2011 11:00:06 AM

```On 28/01/11 5:00 AM, Pekka Kumpulainen wrote:
> "Paulo Silva" wrote in message <ihtt7o\$85t\$1@fred.mathworks.com>...
>> "pixel" wrote in message <ihts5v\$8vq\$1@fred.mathworks.com>...
>> > hi,
>> > I'm trying to create a matrix of 8X12 by the code below but I get a
>> matrix of 12X12.
>> > does anyone understands where is my mistake?
>> > > genes_val=zeros(8,12)
>> > val_ind=1
>> > for j=1:12;
>> > for i=1:8;
>> > genes_vals(i,j)=mean_qty(val_ind)
>> > val_ind=val_ind+1
>> > end
>> > end
>> > > Thanks!
>>
>> it's working just fine genes_vals is 8X12
>>
>> you also don't need the ; in the for line, for j=1:12; is the same as
>> for j=1:12
>
> Instead you might want to add ; in the end of all other lines.
> I found no mistakes in the code, assuming you really want 8 x 12 matrix.
> More "MATLAB-type" code producing the same result could be:
> genes_val=zeros(8,12);
> genes_val(:) = mean_qty;
> or
> genes_val=reshape(mean_qty,8,12);

In order to be equivalent to the original code, mean_qty would have to
be an array or a vectorized function, and the references to mean_qty
would have to be changed to mean_qty(1:8*12)
```
 0
Reply Think 1/28/2011 12:00:25 PM

3 Replies
682 Views

Similar Articles

12/5/2013 6:59:03 AM
page loaded in 2387 ms. (0)