MATLAB Answers

How to extract column information of a mix cell array! - Please Help :(

18 views (last 30 days)
Hi the wonderful MatLab community!
Im very new to MatLab and urgently require some assistance please!
I have a cell array
1 '100'
2 '200'
3 '200'
4 '100'
5 '200'
The cell array changes will always have the two columns but will have random number of rows. The first column will always start from 1 and increase in sequential order moving down the cell array. The second column will only have numbers of either '100' or '200'.
How do you convert array of C such that C(:,2) == 200; basically outcome of array D to be:

Accepted Answer

Henry Giddens
Henry Giddens on 2 Sep 2017
In this example, all of your '100' and 200' values are strings, so you need to identify the cells in the second column of your cell array which have the string value of 200.
The following line of code does this, and also groups the output into an array of numerical values (rather than a cell array).
D = [C{strcmp(C(:,2),'200'),1}]'

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 2 Sep 2017
  1 Comment
Wilson Weng
Wilson Weng on 3 Sep 2017
Thank you so much for your answer as well champ :)
I do have one more question (sorry to be of a hassle)
How would you code the condition where the new cell array
1 '200' NaN (are NaN's as no object are in next to 1, 2 and 3)
2 '100' NaN
3 '200' NaN
4 '200' 1
5 '100' 2
6 '100' 3
7 '200' 4
8 '200' 5
9 '200' 6
10 '100' 7
11 '100' 8
12 '100' 9
... etc
The array will always be a set number of 3 columns but random number of rows. The NaN's on the third column can just be ignored (assumed zeros)
Essentially the way the array works is this:
  • The first column is the number of a specific object.
  • The second column is in reference to only the first column; and is the string 'label' to the specific objects adjacent in the first column . eg looking at column 1; 1 is a '200', 2 is a '100', 3 is a '200' etc.
  • The third column is the specified numbered object which is independent to column 1 but the referencing of this column uses column 1.
  • for example looking at the 4th row of cell array C; for object number 4 (first column) it is a '200' (from reference column 1 and 2 - row 1), the object for number 1 (third column) is also a '200' (from reference column 1 and 2 -row 1).
  • Another example look at row 11: object number 11 (column 1) is a '100' and object number 8 (column 3) is a '200'. Column 3 is a '200' from using the reference columns 1 and 2.
How would you code it such that the condition is true for both columns 1 and 3 of a specific row:
  • the object number (column 1) is a '200' AND the object number (column 3) when referenced using column 1 and 2 is a '200' as well.
with an output in the form of a numerical vector values so that I can use a for loop to make changes to the objects that are true to both the conditions above.
Example of Output cell array which meets the conditions:
4 7 ... etc
I understand this is quite a complex condition and would really really really appreciate some help on this as I'm about to go crazy from spending all day on this.. :(
Kind Regards, Wilson.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by