Matlab: Matching Cell and Double Arrays in Cells

In summary, the user is trying to match values in a cell array with a double array and use ismember to select certain values based on the number of 'C's and values in the double array. They are unsure of how to use the 3D notation in their code.
  • #1
hoffmann
70
0
how do i match up values in a cell array with a double array? for example:

i have a cell array wordmc with the following values:

C1C5
C4C48C279C12526
C13C58C499
C47C479C579
...(7000 values)

i would like to match these values up to another column double of 7000x1 total_table:

2
4
3
1
...

so the first row of the cell C1C5 would have a corresponding value of 2, and so on.

i would like to take the values of the cell array with 2 'C' and a 2 in total_table and match them up to another cell array MAP1 (the same structure as wordmc) using ismember. i would like to take the values of the cell array with 3 'C' tags and a 3 in total_table and match them up to MAP1 using ismember. i know how to count the number of 'C's using the following code:

numC = cellfun(@(x) numel(strfind(x,'C')), wordmc)


i just don't know how to select the ones with a 3 or a 2 in total_table. i think the following pseudocode will work:

numC = cellfun(@(x) numel(strfind(x,'C')), wordmc)
wordmc(numC =3D=3D 2 & total_table =3D=3D 2)

the thing is, i have no idea what 3D notation is. what function does that require and what do i replace the 3D with? thanks!
 
Physics news on Phys.org
  • #2
The 3D notation is not a function, it simply means "equal to"; so you would replace the 3D with a single equal sign (=). The syntax for your code would be: wordmc(numC == 2 & total_table == 2)
 
  • #3


I would suggest using the built-in function "ismember" in MATLAB to match up values in a cell array with a double array. This function allows you to specify the values you want to match and returns a logical array that indicates which elements in the first input array are also present in the second input array.

In this case, you can use the "ismember" function to match the values in the cell array "wordmc" with the values in the double array "total_table". The syntax would be:

idx = ismember(wordmc, total_table);

This will return a logical array "idx" that indicates which elements in "wordmc" are also present in "total_table". You can then use this logical array to select the corresponding values in "wordmc" and "MAP1" using indexing.

For example, to select the values in "wordmc" with 2 'C' tags and a value of 2 in "total_table", you can use the following code:

wordmc_2 = wordmc(idx & numC == 2 & total_table == 2);

Similarly, to select the values in "wordmc" with 3 'C' tags and a value of 3 in "total_table", you can use:

wordmc_3 = wordmc(idx & numC == 3 & total_table == 3);
 

1. How do I match cell and double arrays in Matlab?

To match cell and double arrays in Matlab, you can use the cell2mat function. This function converts the cell array into a regular array, which can then be easily matched with a double array.

2. Can I use cell and double arrays in the same operation?

Yes, you can use cell and double arrays in the same operation by first converting the cell array into a regular array using the cell2mat function.

3. What is the difference between a cell array and a regular array?

A cell array in Matlab can hold elements of different types, while a regular array can only hold elements of the same type. Cell arrays are also more versatile and can store more complex data structures.

4. How do I access elements in a cell array?

You can access elements in a cell array using curly brackets {}, just like you would access elements in a regular array using square brackets []. For example, myCell{1,2} would access the element in the first row and second column of the cell array.

5. Can I convert a regular array into a cell array?

Yes, you can convert a regular array into a cell array using the num2cell function. This function converts each element in the array into a cell, creating a cell array with the same dimensions as the regular array.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • Advanced Physics Homework Help
Replies
19
Views
941
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top