Create new variable that recognizes changes in the code

I have a double matrix B that has in C1 the year 1998 and in C2 a code (unrepeated). Each C2 is given a value in C3.
% C1 C2 C3
B=[ 1998 22 37
1998 34 7
1998 76 12
1998 98 29
1998 107 14
]
This is how I got to B:
N1=[N{:,1} N{:,2} N{:,3}];
N1=unique(N1(:,1:3),'rows');
N3= unique(N1(:,1:2),'rows');
for m=1:size(N3,1)
N3(m,3)=sum(N1(:,1) == N3(m,1) & N1(:,2)==N3(m,2));
end
B=N3((N3(:,1) == 1998),:);
I have a cell-array A with the years horizontally disposed in R1, un-repeated values in Y, and corresponding codes in the columns that follow. The codes are the same as in C2 from variable B, but disposed differently.
A={Y 1996 1997 1998 1999 %R1
1 107 107 22 22
13 98 98 76 1267
}
Is there a way I could get a new variable that recognizes the change in the codes in variable A, and presents the corresponding values from C3 in B? For instance:
AB={Y Initial C2 Change C2
1 107 14 22 37
13 98 29 76 12 }

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 22 Jul. 2014
Given the A and B from above (less the incomplete portions), you could iterate over each row of A and compare the second and fourth values with that from distinct second column of B, looking for a match. If a match is found, then replace the third and fifth values form AB with those from B (if a match for both can be found)
% initialize AB to that of A
AB = A;
% iterate over each row of A
for k=1:size(A,1)
% search the second column of B for the second element of A
idx = find(B(:,2)==A{k,2});
if ~isempty(idx)
% replace
AB{k,3} = B(idx,3);
else
% not found, so set to empty (?)
AB{k,3} = [];
end
% search the second column of B for the fourth element of A
idx = find(B(:,2)==A{k,4});
if ~isempty(idx)
% replace
AB{k,5} = B(idx,3);
else
% not found, so set to empty (?)
AB{k,5} = [];
end
end
Try the above and see what happens!

7 Kommentare

Hello Geoff. I tried your code, it gives me the following error:
'Error using ==
Matrix dimensions must agree.
Tomorrow I will review the code and see what may be wrong. Or maybe it's my data. I have to check. But thank you very much.
Hi Maria - which line of code caused the error? My A and B were almost similar to yours though the A did not have the header row of Y 1996 1997 1998 1999.
Maria
Maria am 23 Jul. 2014
I am not being able to identify which line exactly caused the error, because when I run each line separately it works and only at the end of the loop it says error. So I am guessing the loop works for the first X rows and then somewhere gives the error 'matrix dimensions must agree'. My double matrix B has 321 rows while my cell array A has 4000 rows. Do you think it may have something to do with the difference in the rows? Thank you.
Maria
Maria am 23 Jul. 2014
Geoff, I was thinking, it is possible that not all codes in A are in B, that may be a problem right?
Maria - if a code is in A but not in B then the else block will be evaluated (for either case).
Try the adding the following line
dbstop if error
as the first line in your script, just prior to the initialization of
AB = A;
When an error in the code is encountered, this line will cause the debugger to open and you can see exactly which line is causing the problem, what the value of k is (i.e. which row in A), what the codes are, etc.
Maria
Maria am 24 Jul. 2014
Finally it's working! Thanks for the help!
Great!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 22 Jul. 2014

Kommentiert:

am 24 Jul. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by