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:
avoiding loops using repmat

Subject: avoiding loops using repmat

From: Jose

Date: 25 Mar, 2009 11:03:01

Message: 1 of 5


 Hi everyone, I have the next code:
     
x=[2 3; 4 5; 1 4] % 2D- dimensional data
a=[2 3; 3 2] % 2D- dimensional parameter

and I want to built a z matrix as:

x(1,:)-a(1,:) x(1,:)-a(2,:)
x(2,:)-a(1,:) x(2,:)-a(2,:)
x(3,:)-a(1,:) x(3,:)-a(2,:)

One way to try doing it is:

z1=x-repmat(a(1,:),nrp,1)
z=repmat(z1,1,nrv)

z =

     0 0 0 0
     2 2 2 2
    -1 1 -1 1

The structure of the matrix z is ok, but I need change the values of the
2nd block (3rd and 4th column) to obtain the right matrix, but I do not know how I can do it if I want to extend it to N blocks.

Can you help me?

Thanks in advance.

Jose.

Subject: avoiding loops using repmat

From: us

Date: 25 Mar, 2009 11:15:11

Message: 2 of 5

"Jose"
> Hi everyone, I have the next code:
> x=[2 3; 4 5; 1 4] % 2D- dimensional data
> a=[2 3; 3 2] % 2D- dimensional parameter
> and I want to built a z matrix as:
> x(1,:)-a(1,:) x(1,:)-a(2,:)
> x(2,:)-a(1,:) x(2,:)-a(2,:)
> x(3,:)-a(1,:) x(3,:)-a(2,:)
> The structure of the matrix z is ok, but I need change the values of the
> 2nd block (3rd and 4th column) to obtain the right matrix, but I do not know how I can do it if I want to extend it to N blocks...

this is not ...the next code...
it is the same problem for which you got several replies yesterday...
did you bother to look at them...

us

Subject: avoiding loops using repmat

From: Jose

Date: 25 Mar, 2009 11:30:05

Message: 3 of 5

"us " <us@neurol.unizh.ch> wrote in message <gqd3nv$3cp$1@fred.mathworks.com>...
> "Jose"
> > Hi everyone, I have the next code:
> > x=[2 3; 4 5; 1 4] % 2D- dimensional data
> > a=[2 3; 3 2] % 2D- dimensional parameter
> > and I want to built a z matrix as:
> > x(1,:)-a(1,:) x(1,:)-a(2,:)
> > x(2,:)-a(1,:) x(2,:)-a(2,:)
> > x(3,:)-a(1,:) x(3,:)-a(2,:)
> > The structure of the matrix z is ok, but I need change the values of the
> > 2nd block (3rd and 4th column) to obtain the right matrix, but I do not know how I can do it if I want to extend it to N blocks...
>
> this is not ...the next code...
> it is the same problem for which you got several replies yesterday...
> did you bother to look at them...
>
> us

Hi us, did you bother to look at my reply a did last night to you?

Subject: avoiding loops using repmat

From: us

Date: 25 Mar, 2009 12:53:01

Message: 4 of 5

"Jose"
> Hi us, did you bother to look at my reply a did last night to you...

yes, i did, but then i saw this new post starting all over again...

one of the solutions
- based on what you've been shown before...

     nrp=5;
     nrv=3;
     v1=ceil(10*rand(nrp,2));
     v2=ceil(5*rand(nrv,2));
     [ix2,ix1]=meshgrid(1:nrv,1:nrp);
     r=arrayfun(@(x,y) v1(x,:)-v2(y,:),ix1,ix2,'uni',false);
     [r{1,:}] % <- first row
     [r{nrp,:}] % <- last row
     m=cell2mat(r) % <- matrix
%{
% note: random data!
     5 1 4 -3 1 -2 % <- first row
     2 5 1 1 -2 2 % <- last row
% mat
     5 1 4 -3 1 -2 % <- first row
     2 2 1 -2 -2 -1
     6 3 5 -1 2 0
     7 4 6 0 3 1
     2 5 1 1 -2 2 % <- last row
%}

us

Subject: avoiding loops using repmat

From: Jose

Date: 25 Mar, 2009 13:10:03

Message: 5 of 5

"us " <us@neurol.unizh.ch> wrote in message <gqd9fd$p24$1@fred.mathworks.com>...
> "Jose"
> > Hi us, did you bother to look at my reply a did last night to you...
>
> yes, i did, but then i saw this new post starting all over again...
>
> one of the solutions
> - based on what you've been shown before...
>
> nrp=5;
> nrv=3;
> v1=ceil(10*rand(nrp,2));
> v2=ceil(5*rand(nrv,2));
> [ix2,ix1]=meshgrid(1:nrv,1:nrp);
> r=arrayfun(@(x,y) v1(x,:)-v2(y,:),ix1,ix2,'uni',false);
> [r{1,:}] % <- first row
> [r{nrp,:}] % <- last row
> m=cell2mat(r) % <- matrix
> %{
> % note: random data!
> 5 1 4 -3 1 -2 % <- first row
> 2 5 1 1 -2 2 % <- last row
> % mat
> 5 1 4 -3 1 -2 % <- first row
> 2 2 1 -2 -2 -1
> 6 3 5 -1 2 0
> 7 4 6 0 3 1
> 2 5 1 1 -2 2 % <- last row
> %}
>
> us

Dear us, Thank you very much!!!
You solved my problem.
Cheers,
Jose.

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