Extraction of row information of a mixed cell array with strings based on multiple conditions.
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Stephen23
2017-9-3
编辑:Stephen23
2017-9-3
"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 个评论
Stephen23
2017-9-4
编辑:Stephen23
2017-9-4
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).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!