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:
Creating a matrix from arrays generated in for-loop

Subject: Creating a matrix from arrays generated in for-loop

From: ryan

Date: 28 Jun, 2010 00:27:07

Message: 1 of 2

Hi!
When I run this loop I obtain 5 simultaneous 'z=' statements:
e.g.:

z=
   1 2 3 4 5
z=
   2 9 8 7 6

is there a way to generate one matrix 'Z' with 'n' rows and the '5' colums out of these five arrays instead?
ex:

z=
   1 2 3 4 5
   2 9 6 7 8
...
loop portion of code:

for k=1:5
    
    
    for h=1:n
        i=1:n;
        j=1:n;
        
        T=Dz(i,j)*X;
        
        S=sum(T');
        
        C(i);
        
        U=(C(i)-T(i));
        d;
        h;
        X(h)=U(h)/d(h);
        
    end
    
    Z=[k;X]'
    
    for h=n;
        k;
        h;
        X';
        
    end
end

X=X';


Thanks in advance!

Subject: Creating a matrix from arrays generated in for-loop

From: Faraz Afzal

Date: 28 Jun, 2010 01:52:07

Message: 2 of 2

"ryan " <ryanlendslasvegas@yahoo.com> wrote in message <i08q8r$mn6$1@fred.mathworks.com>...
> Hi!
> When I run this loop I obtain 5 simultaneous 'z=' statements:
> e.g.:
>
> z=
> 1 2 3 4 5
> z=
> 2 9 8 7 6
>
> is there a way to generate one matrix 'Z' with 'n' rows and the '5' colums out of these five arrays instead?
> ex:
>
> z=
> 1 2 3 4 5
> 2 9 6 7 8
> ...
> loop portion of code:
>
> for k=1:5
>
>
> for h=1:n
> i=1:n;
> j=1:n;
>
> T=Dz(i,j)*X;
>
> S=sum(T');
>
> C(i);
>
> U=(C(i)-T(i));
> d;
> h;
> X(h)=U(h)/d(h);
>
> end
>
> Z=[k;X]'
>
> for h=n;
> k;
> h;
> X';
>
> end
> end
>
> X=X';
>
>
> Thanks in advance!

Hi ryan..

Yes you can do this .. Just add a little bit more to your for loop..

Such as the part that I have mentioned with ">>>>" You can change it to columns using transpose or leaving transpose..

for k=1:5
    
    
    for h=1:n
        i=1:n;
        j=1:n;
        
        T=Dz(i,j)*X;
        
        S=sum(T');
        
        C(i);
        
        U=(C(i)-T(i));
        d;
        h;
        X(h)=U(h)/d(h);
        
    end
   >>>>> Z = zeros(n,k);
   >>>>> Z(:,k)=[k;X]' ;
    
    for h=n;
        k;
        h;
        X';
        
    end
end

X=X';

I hope it help..

Let me know if it worked..

Regards,
Faraz

Tags for this Thread

No tags are associated with 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