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:
Problem using horzcat with index vectors in a for loop

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 20 Jan, 2013 23:22:07

Message: 1 of 13

Hi
I am trying to horizontally concatenate identical rows identified from two matrices using index vectors obtained using intersect: E.g.
A =
     2 4
     5 7
     2 8
     3 1
     8 4
B =
     5 7
     2 4
     2 2
     8 4
     3 1
[C,ia,ib] = intersect(A,B,'rows', 'legacy');
C =

     2 4
     3 1
     5 7
     8 4
ia =
     1
     4
     2
     5

ib =
     2
     5
     1
     4

So I want to use the vector indices (ia / ib) to horizontally concatenate the matched rows found using intersect. E.g. for the first identified row this would be:
 D = horzcat(A(ia(1),:), B(ib(1),:))

However, I am struggling to find the syntax to repeat this procedure iteratively (i.e. using a for loop). Note that the length of A, B and C are unknown though rows will always be two variables long. I have tried the following:

for i=1:length(ia)
    D = horzcat(A(ia(i),:),B(ib(i),:));
end

But this only concatenates the first value identified in C. I've tried a few variations without success. Help woould be appreciated.
Thanks in advance
Thomas

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 21 Jan, 2013 11:02:09

Message: 2 of 13

Note that the code works through the list and concatenates the correct matching rows. However, D is overwritten with each iteration. I would like each concatenated row to be added to a new m x 4 matrix where the number of rowsand order of matched columns is equal to those in C (see above). I am using preallocation to speed things up:

[m,n] = size(C);
D = zeros(m,n*2);

Perhaps cell arrays might offer a better solution (I am still learning the use of these though)?
Thomas

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 21 Jan, 2013 11:13:08

Message: 3 of 13

Hi
I have come up with a solution, though it still might not be the best:

[m,n] = size(C);
D = zeros(m,n*2); %pre-allocate
for i=1:length(C)
    D(i,:) = horzcat(A(ia(i),:), B(ib(i),:))' %add rows to D iteratively and transpose
end

Maybe cell arrays are a quicker option?
Thomas

Subject: Problem using horzcat with index vectors in a for loop

From: Bruno Luong

Date: 21 Jan, 2013 13:06:08

Message: 4 of 13

"Thomas " <thomas.seers@postgrad.manchester.ac.uk> wrote in message <kdhu6v$o6p$1@newscl01ah.mathworks.com>...
> Hi
> I am trying to horizontally concatenate identical rows identified from two matrices using index vectors obtained using intersect: E.g.
> A =
> 2 4
> 5 7
> 2 8
> 3 1
> 8 4
> B =
> 5 7
> 2 4
> 2 2
> 8 4
> 3 1
> [C,ia,ib] = intersect(A,B,'rows', 'legacy');
> C =
>
> 2 4
> 3 1
> 5 7
> 8 4
> ia =
> 1
> 4
> 2
> 5
>
> ib =
> 2
> 5
> 1
> 4
>
> So I want to use the vector indices (ia / ib) to horizontally concatenate the matched rows found using intersect. E.g. for the first identified row this would be:
> D = horzcat(A(ia(1),:), B(ib(1),:))

Please post your whole code (with ismember) and short example of desired output. It sounds like you just concatenate two times C together.

Bruno

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 21 Jan, 2013 21:56:08

Message: 5 of 13

Hi Bruno
I am not using 'ismember' to acquire the indices, I am actually using intersect, as I need the locations of intersecting rows (this is so I can match equivalent but not matching data stored in a vector of equal length to :
INPUTS
A =

     2 4
     5 7
     2 8
     3 1
     8 4
B =

     5 7
     2 4
     2 2
     8 4
     3 1

[C,ia,ib] = intersect(A,B,'rows', 'legacy'); % find matched rows

C = %matched rows

     2 4
     3 1
     5 7
     8 4
ia = % A row indices

     1
     4
     2
     5

ib = %B row indices

     2
     5
     1
     4

[m,n] = size(C);
catcam = zeros(m,n*2);
for i=1:length(C)
    D(i,:) = horzcat(A(ia(i),:), B(ib(i),:))'; % concatenate matched rows using indices
end

This code essentially gives me the output I wanted, matched rows concatenated to form an m x 4 array:
OUTPUT
D =

     2 4 2 4
     3 1 3 1
     5 7 5 7
     8 4 8 4
This output is fine though it runs a bit slow on big datasets. However, the next bit of code is a bit problematic. I have a vector, storing values (x1,..,n) asscociated with each row in D. However, these values are duplicate, with one value in x corresponding to many rows in D. This is because in it's original format, x was a tag at the start of a row containing several (image coordinates) which form A and B. The number of image coordinate pairs that correspond to each value in x are also known (ind1,..,indn). This number is not a costant:

i.e.
Data =
[x1] [ind1] [xy1] [xy2] [xy3] 0 0
[x2] [ind2] [xy4] [xy5] [xy6] [xy7] [xy8] etc

ind and xy have been reshaped into column vectors to allow easy manipulation. I now need to create the duplicates of x to match the number of image coordinates contained in the rows in data to produce a matrix of the form (using the above as a template):

Desired =
x1 x1 x1 0 0
x2 x2 x2 x2 x2
With zeros / NaNs used to pad out to the maximum row length (minus x and ind) in data.
To do this I use repmat

null = 0;
IC = data(:,3:end);
[row, col] = size(IC);
desired = zeros(row, col);
for k=1:length(col)
    desired(k,:) = vertcat(repmat(x(k), ind(k),k),repmat(null, col-ind(k),k))';
end

As far as I can tell, this should give the right output, but I get a correctly sized matrix back with the right output in the first row, but only empty zeros for the others.
Apologies for the long post (it jumped ahead a bit admittedly). I'm sure this is just a fairly minor syntax error.
Thomas

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 21 Jan, 2013 22:09:09

Message: 6 of 13

Update: the problem was a syntax error: it should have read:

for k=1:length(x)
    desired(k,:) = vertcat(repmat(x(k), ind(k),1),repmat(null, col-ind(k),1))';
end

This gives the correct output.
Thomas

Subject: Problem using horzcat with index vectors in a for loop

From: Bruno Luong

Date: 21 Jan, 2013 23:41:09

Message: 7 of 13

"Thomas " <thomas.seers@postgrad.manchester.ac.uk> wrote in message <kdkdho$pk3$1@newscl01ah.mathworks.com>...
> This code essentially gives me the output I wanted, matched rows concatenated to form an m x 4 array:
> OUTPUT
> D =
>
> 2 4 2 4
> 3 1 3 1
> 5 7 5 7
> 8 4 8 4
> This output is fine though it runs a bit slow on big datasets.

As I suspect, D is equal to [C C] or repmat(C,[1 2]). Why bother with ia, ib????
 
I don't understand the rest of your post, with x, xy, ind etc...

If you want to reconstruct D from A, and B, it's just [A(ia,:) B(ib,:)], but again it's the same as [C C].

Bruno

Subject: Problem using horzcat with index vectors in a for loop

From: nl.doc@arcor.de

Date: 22 Jan, 2013 10:18:46

Message: 8 of 13

Does the following line execute the correct task?
Independently of the dimension of A and B (only number of columns should be the same).

desired=[A(ia,:),B(ib,:)];

But I don't know why doubling the columns, because per definition
A(ia,:) is identically B(ib,:) when using intersect.

Regards
Norbert

Subject: Problem using horzcat with index vectors in a for loop

From: Bruno Luong

Date: 22 Jan, 2013 10:31:08

Message: 9 of 13

nl.doc@arcor.de wrote in message <7db9a287-0e77-4367-94e1-a7e73a20411c@googlegroups.com>...
> Does the following line execute the correct task?
> Independently of the dimension of A and B (only number of columns should be the same).
>
> desired=[A(ia,:),B(ib,:)];

A(ia,:) is equal to B(ib,:) and is equal to C, regardless the number of rows of A and B.

>
> But I don't know why doubling the columns, because per definition
> A(ia,:) is identically B(ib,:) when using intersect.

Don't understand your question. They are identical,, therefore you just duplicated C twice on the horizontal dimension. I though I make it clear in my previous post.

Bruno

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 22 Jan, 2013 11:37:08

Message: 10 of 13

Apologies for the lack of clarity. D is equal to C C. The concatenation of matching rows from A and B was only needed to tell me if INTERSECT would give me the correct locations of non matching data (i.e. ind1,..,n / x1,..,n) that was originally stored in the same rows as the image coordinates (i.e. A and B which does contain some matching data). I was trying to arrive at a point where I could retrieve data which is non matching from column vectors of the same length as A and B (this is what the second bit was about). However, I have managed to work through to this point in the end. Sorry again. I am quite new to programming, so I get stuck on syntax quite a bit.
Apologies for my foolishness!
Thomas

Subject: Problem using horzcat with index vectors in a for loop

From: Bruno Luong

Date: 22 Jan, 2013 11:45:09

Message: 11 of 13

That's the reason we keep ask for meaningful and short example when a specific question is asked. If A and B have non matching columns, you should be provided in the example. It is much better than 1000 words.

Bruno

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 22 Jan, 2013 15:51:08

Message: 12 of 13

Yes, you are right. This is an unnecessary step. I only need the indices from intersect to concatenate the non-matching components of my data stored in equivalent length vectors. I have now achieved this using

vmatch = zeros(m,n*6);
for i=1:length(C)
    vmatch(i,:) = horzcat(vectors(ia(i),:), vectors2(ib(i),:))';
end

where vectors and vectors2 are m x 6 matrices storing non-matching data relating to each row in A and B respectively.
Thanks again for your input.
Thomas

Subject: Problem using horzcat with index vectors in a for loop

From: Thomas

Date: 22 Jan, 2013 16:16:09

Message: 13 of 13

Yes, apologies again for my lack of clarity.
Original data:

originalA =
     2 4 3.1 2.5
     5 7 3.4 3.7
     2 4 6.7 2.8
     3 1 3.3 7.6
     8 4 4.6 6.7

originalB =
     5 7 5.8 9.5
     2 4 5.1 4.5
     2 2 4.2 3.5
     8 4 5.5 3.8
     3 1 4.2 1.3

%Segment

A = originalA(1:2,:)
B = originalB1:2,:)
vectors = originalA(3:4,:)
vectors2 = originalB(3:4,:)

A =
     2 4
     5 7
     2 8
     3 1
     8 4
B =
     5 7
     2 4
     2 2
     8 4
     3 1

vectors =
3.1 2.5
3.4 3.7
6.7 2.8
3.3 7.6
4.6 6.7

vectors2 =
5.8 9.5
5.1 4.5
4.2 3.5
5.5 3.8
4.2 1.3

%find intersections in A and B

C = %matched rows

     2 4
     3 1
     5 7
     8 4
ia = % A row indices

     1
     4
     2
     5

ib = %B row indices

     2
     5
     1
     4

[C,ia,ib] = intersect(A,B,'rows', 'legacy'); % find matched rows

% Match non matching data using the indices found in the previous step

vmatch = zeros(4,4);
for i=1:length(C)
    vmatch(i,:) = horzcat(vectors(ia(i),:), vectors2(ib(i),:))';
end

This is the the problem I was trying to solve. Sorry for the confusion!
Thomas

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