How can I matching data from different matrices

5 views (last 30 days)
Hi!
I have 2 matrices [A], [B], with different dimensions ([A]=4450x10 & [B]=3656x5). The first column in both matrices is the day with numbers 1:365. But some days are missing, not the same days from the two matrices. Also,for each day I have different number of data. For example, in the day 15 in [A] I have 12 lines and in the [B] I have 4 lines. I found the common days:
Common_days = intersect(A(:,1),B(:,1));
Then, I want to create two files (one for each matrix), which will include only the data with the common days.
Extra, I want to plot in the same graph, the data with the same day from [A] & [B]. The columns I want to plot are [A]=2 & 10, [B]=2&5

Accepted Answer

Stephen23
Stephen23 on 21 Mar 2015
Edited: Stephen23 on 21 Mar 2015
This is easy using ismember. For simplicity I only show the first column:
>> A = [7;1;7;7;4];
>> B = [7;0;4;4;0];
>> A2 = A(ismember(A,B),:)
A2 =
7
7
7
4
>> B2 = B(ismember(B,A),:)
B2 =
7
4
4
You can then save A2 and B2 in the text files using dlmwrite.

More Answers (1)

Image Analyst
Image Analyst on 20 Mar 2015
Use ismember() to find out what rows Common_days are in. Then just use indexing
[ia, ib] = ismember(.......
subsetA = A(rowsToExtract, :);
I can't remember if rowsToExtract is ia or ib but you'll know which it is after you run it.
  1 Comment
Thar
Thar on 21 Mar 2015
For the day 8 i have one more lines with data like:
8 8,98138900000000 2,48300000000000 316,500000000000 8,37422454166667
8 9,12027800000000 2,42400000000000 319,100000000000 8,38001158333333
8 9,57027800000000 2,28200000000000 317,500000000000 8,39876158333333
8 9,74138900000000 2,24400000000000 317,400000000000 8,40589120833333
8 10,0605560000000 2,19400000000000 318,300000000000 8,41918983333333
8 10,5166670000000 2,16400000000000 317,800000000000 8,43819445833333
If i write [ia, ib] it keeps the first line of the day only. I want all lines. This happens in all days.
I wrote :
[Common_days,ia,ib] = intersect(A(:,1),B(:,1));
Then C=A(ia,1) D=B(ib,1) and also keep the first line from the data with the same day.
Can I do something else?
Any ideas?

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!