Extraction of row information of a mixed cell array with strings based on multiple conditions.

3 views (last 30 days)
Hello the wonderful MatLab community,
Im still quite new to MatLab and have spent almost the whole day to no avail in figuring this out;
How would you code the condition where the new cell array
C=
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), object 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:
D=
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.

Accepted Answer

Stephen23
Stephen23 on 3 Sep 2017
Edited: Stephen23 on 3 Sep 2017
"Im still quite new to MatLab and have spent almost the whole day to no avail in figuring this out"
Storing numeric data in a cell array makes your life much harder. So the first thing to do is to put all of the numeric data into numeric arrays, then the solution is quite easy using ismember and some simple indexing.
C = {...
1 '200' NaN
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};
>> obj = vertcat(C{:,1}); % first column -> numeric
>> ref = vertcat(C{:,3}); % third column -> numeric
>> [idx,sub] = ismember(ref,obj); % match values in the two columns
>> idz = strcmp(C(:,2),'200'); % identify '200'
>> out = idx;
>> out(idx) = idz(idx) & idz(sub(idx))
out =
0
0
0
1
0
0
1
0
0
0
0
0
this returns a logical index, which is often more efficient to use than subscript indexing. If you really need to get the subscript indexing then just use find:
>> find(out)
ans =
4
7
>>
  3 Comments
Stephen23
Stephen23 on 4 Sep 2017
Edited: Stephen23 on 4 Sep 2017
Use a semi-colon to suppress printing output in the command window. The Introductory Tutorials teach these kind of very basic and important MATLAB concepts, such as how to suppress displaying output, and the tutorials are highly recommended for all beginners:
The MATLAB editor also highlights any commands which will be printed like this, and shows a warning (which is one of a thousand reasons why I am always telling beginners to pay attention to the editor warnings and code hints).
Wilson Weng
Wilson Weng on 7 Sep 2017
Thanks for the advice with the code analyzer, it has helped heaps!
And I will slowly try and go through the tutorials for MatLab, apologies for the inconvenience.
Have a good day! :)

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!