Clear Filters
Clear Filters

if statement on cell

1 view (last 30 days)
Grigorios Kyritsakas
Grigorios Kyritsakas on 19 Jul 2018
Hi,
I have a matrix A with 3 columns. The first column contains letters (specific reference numbers) and the other two columns contain numbers (values). I also have a big table B that contains also the first column of the matrix A but its length is bigger than A's as some reference numbers are repeated. I want to add two extra columns in table B where for each reference number I get the corresponding to the reference number values of the matrix A. I tried to use the if function but it didn't work (as reference numbers are cells) and isequal is not working as the first column in A has less rows than the columns of table B. Can anyone help me with this?
  3 Comments
Guillaume
Guillaume on 19 Jul 2018
Or if the matrices were converted to tables, which sound better suited for what they contain, a single call to join would do the job.
But yes, an example of the inputs is required, particularly since matrices cannot contain both text and numbers, so it's unknown what A and B actually are.
Grigorios Kyritsakas
Grigorios Kyritsakas on 19 Jul 2018
Hello and thanks to both of you.
A is a table not a matrix - apologies for that mistake. Let's say that A =
5×3 table
NODEID AW2 AP2
____________ ______ ______
'000 440 01' 27.95 48.78
'000 480 01' 67.09 31.11
'000 656 01' 89.25 41.11
'001 479 08' 65.91 35.3
'001 479 13' 65.5 35.04
B =
9×4 table
Sample ID GL D
__________ ____________ ______ ______
7.4602e+06 '000 440 01' 54.1 131.9
1.0423e+07 '000 440 01' 54.1 131.9
1.0451e+07 '000 440 01' 54.1 100.22
6.6269e+06 '000 480 01' 49.1 17.475
8.0283e+06 '000 656 01' 6 505.4
1.0383e+07 '000 656 01' 6 505.4
8.373e+06 '001 479 08' 44.576 34.732
7.7677e+06 '001 479 13' 44.84 19.308
6.9789e+06 '001 479 18' 45.58 33.286
ID column in table B contains the reference numbers that are also included in NODEID in table one. I want the numbers of the columns AW2 and AP2 of table to be added as an extra column in the corresponding reference number in table B.
Thanks again

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 19 Jul 2018
Edited: Guillaume on 19 Jul 2018
Since your inputs are tables, it's trivial to merge the two tables as you want:
C = outerjoin(B, A, 'LeftKeys', 'ID', 'RightKeys', 'NODEID', 'MergeKeys', true)
Possibly, you want an innerjoin instead of an outerjoin if you don't want to retain the rows of B that have no match in A.
  1 Comment
Grigorios Kyritsakas
Grigorios Kyritsakas on 19 Jul 2018
Perfect! That worked!
Thank you very much for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!