Finding contents of cell in another one

Hi I have a matrix that is called file1=[1109x1] and another matrix that is caled rxn=[1170x7] i need to know if the contents of the 'file1' exist in column#5 of the rxn and if the answer is yes, the whole content of the related row in 'rxn' would be copied to a new matrix, what should i do? Please help me on this issue Thank you alot!
[EDITED, Jan, moved from comment sections]
Here is what my data exactly is: File1 contains 1column x 1109rows and each cell contains numbers like this: 2.11.7.8
Rxn contains 7column x 1170rows. In every cells of column#5 exists numbers like what it was in file1 (2.11.7.8) I am going to compars every cells of file1 with cells of rxn column#5 and if the numbers were exactly the same, the whole content of the related row in rxn would be copied to a new matrix Please note that i need the exact properties of each cell

10 Comments

Do you want to know if the entire column matches, or if each element of file1 matches any value in rxn?
Each element of file1 matches any value in rxn
After moving the comment to the question:
@babak: Please do not post important information about the type of the input as 3 identical comments, but insert it in the question. The question is the location, where all important information should and must be found.
But I tend to give up. "2.11.7.8" is not a number. Numbers can contain a single decimal dot only. Perhaps it is a string, but even then the code I have posted should work correctly.
So dont you have any solution for my question?
Please help me by a useful solution or inform me about a useful source or reference that could help me
@babak: I have posted a solution yesterday, Matt Tearle has posted almost the same solution some seconds later also. The only problem is, that you do not explain the type of your inputs exactly. But all information, you have provided yet, imply, that the posted solutions should work already. As far as I understood, you did not get my suggestion to solve your problem, because you have copied the lines, which create the test data. But these two lines have only be included, because you didn't specify a valid set of input data by your own.
Please try the posted solutions again - carefully.
i think i specified every thing about my issue in my comments, what should i describe about that?
What have you tried so far in solving this problem. Where are you stuck. Can you create for us a small version of file1 and rxn and then tell us the output you want. Make sure the example data is rich enough to hit on all edge cases you are interested in. Without this we can only guess at the answer.
babak
babak on 21 Sep 2012
Edited: babak on 21 Sep 2012
i used this code but it didnt work:
for i=1:1170
g=find(ismember(rxn{i},file1{i}));
end
if numel(g)>0
for j=1:5
x=(rxn(5));
s=[s(x);:];
end
end
rxn is a 2-dimensional cell array, so you should be indexing into the 5th column, which you are not. However, you should not need to do this in a loop. The code I provided earlier should work. If not, please explain what isn't working and why. For starters, what do you get if you do this:
idx = ismember(rxn(:,5),file1);
nnz(idx)

Sign in to comment.

Answers (4)

Jan
Jan on 19 Sep 2012
Edited: Jan on 19 Sep 2012
file1 = randi(1000, 1, 1109); % Test data
rxn = randi(1000, 1170, 7);
match = ismember(rxn(:, 5), file1);
result = rxn(match, :);

6 Comments

It creats a matrix but the contents are not the contents of rxn
Jan
Jan on 19 Sep 2012
Edited: Jan on 19 Sep 2012
So? Isn't it? I'm sure that rxn(match, :) replies the contents of rxn. Did you overwrite your data by my random test data?
If you need another order, you can use the 2nd or 3rd output of ISMEMBER.
In description of the function'randi' it told that it creats a random numbers, this is exactly what i'm gained, but i need the exact contnts of rxn!!!!!
What do you mean they're not the contents of rxn? result is created by indexing into rxn, so there's no way the contants of result can be anything but the contents of rxn!
The contents of rxn are some words, but this code gives me some numbers
Jan
Jan on 19 Sep 2012
Edited: Jan on 19 Sep 2012
In your question I find "rxn=[1170x7]", which usually means that the data is a matrix of type double. If you use a different input, it would be a really good idea to explain this explicitely. Otherwise guessing what you are looking for wastes your and my time.
Of course my method replies numbers. I used numbers as test data as explained in the comment. It should not be to hard to omit the two lines containing randi, such that your data are processed.

Sign in to comment.

You could use ismember and indexing values to determine which rxn columns have corresponding values within file1 but this can also be solved using a set of loops as shown:
count= 0;
for i = 1 : length(file1)
for j = 1 : length(rxn)
if file1(i) == rxn(j,5)
count = count + 1;
finalMatrix(count,:) = rxn(j,:);
end
end
end
The finalMatrix will be a matrix containing all of the rows of rxn that have their column 5 values identified within file1

6 Comments

I used the exact codes that u wrote here, but it didnt work, after the third end it shows an error : ??? Index exceeds matrix dimentions
A pre-allocation will improve the efficiency: Allocate finalMatrix by zeros(length(file1), 5)) and cut off undefined values at the end: finalMatrix = finalMatrix(1:count, :).
@jane: could you please write me the complete codes?
But the posted code is complete already.
Do you meane i should add yor allocations to shane's code? If yes at which steps?
Shane's code assumes also, that your data are double matrices. If you are talking about cell strings, please specify this. A small set of example data would be a good idea also.

Sign in to comment.

If I understand your intent correctly, you want every row of rxn for which the value in the 5th column is one of values in the vector file1. If so:
result = rxn(ismember(rxn(:,5),file1),:);

7 Comments

It says that index exceed matrix dimentions
My code is exactly the same as Jan's (sorry, Jan, didn't see you had already posted). This works fine for me:
file1 = randi(1000, 1, 1109);
rxn = randi(1000, 1170, 7);
result = rxn(ismember(rxn(:,5),file1),:);
Have the contents or dimensions of you variables changed? Did you try this on the data freshly imported?
When rxn has at least 5 columns, the error message is not possible. Please post the code you are really using.
Sory, but it creats random numbers, not exact contents of rxn!
Sory jan, that was another problem, that was misunderestanding;)
As Jan explained, the lines
file1 = randi(1000, 1, 1109);
rxn = randi(1000, 1170, 7);
create some test data (because, funnily enough, we don't have access to your actual data). Given your description of your data (an 1109-element vector and a 1170-by-7 matrix),
result = rxn(ismember(rxn(:,5),file1),:);
does what you asked. If it is not working as you expected, you need to explain what you expected, and exactly what your data looks like and what you are doing to it.
BTW, that line of code should work on virtually any data type. However, if you're working with cell arrays, it will depend on the contents. Again, you need to describe your data precisely.
From your description of the data, it sounds a lot like you have a cell array (as Jan says, "2.11.7.8" could not be stored as a numeric type, so it is probably a string, and you mentioned that the contents of rxn are "some words"). If rxn is a 1170-by-7 cell array, and file1 is a 1109-by-1 cell array, then the code I gave should still work.

Sign in to comment.

Javier
Javier on 21 Sep 2012
Edited: Javier on 21 Sep 2012
Hello Babak
Ok big issue here but not impossible. The following procedure works in Matlab R2012a.
Step 1
Import 3 files: One will be Rxn (7 columns), File1 and other called "Var1" will be column 5 of Rxn. When you import this to Matlab you will have string information not data. Because the information is in Excel (for example) use the function num2str in the import process.
Step 2
Now, for each string in File1, we begging the search in Var1. This will work for the search of the first string in File1.
for i=1:1170
R(i,1)=sum(find(A(i,:)==File1(1,:)),2);
end
How R works. For 2.11.7.8 we have 8 characters. Then, if R= 36 all the string match and we find what we are looking for. If you remove the sum, an error will appear. At the end I provide a code that you can check. If the number of characters change, establish a condition in Step 3 according to string data search.
Step 3
R is column vector. Find in R the value 36. Remember, 2.11.7.8 we have 8 characters, and the sum of 1 to 8 is 36.
RR=find(R==36) %Now you have the rows
Step 4
New matrix and copy process
for i=1:size(RR,1)
Newm(i,:)=Rxn(RR(i,1),:)
end
Code
%Step 1 Search a value (A(1,:)). This is the first value in A. It could be the first element of File1
a=randn(100,4);
A=[num2str(abs(round(a(:,1)))),repmat('.11.',100,1),num2str(abs(round(a(:,2))))];
B=[num2str(abs(round(a(:,3)))),repmat('.11.',100,1),num2str(abs(round(a(:,4))))];
%My Rxn matrix (2 columns A,B)
Rxn=[A,B];
Var1=A; %search in this column
File1=A(1,:) %search this data
%Step 2
for i=1:size(Rxn,1)
R(i,1)=sum(find(Var1(i,:)==File1(1,:)),2);
end
%Step 3
RR=find(R==21) %Data are like 1.11.1, 6 characters and sum(1 to 6)=21
%Step 4
for i=1:size(RR,1)
Newm(i,:)=Rxn(RR(i,1),:)
end
Feel free to make a comment and verify that the code supplied works before comment.
Regards
Javier

Asked:

on 19 Sep 2012

Community Treasure Hunt

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

Start Hunting!