How to deal with a vector containing numbers and characters?
Show older comments
Having a vector of mixed numbers and characters like
M=['(1,3)',
'(5,4)',
'(9,7),(6,8)',
'(7,3)',
'(7,9),(2,4),(3,10)',
'(5,8)']
how can I compute the minimal set of numbers S such that each row of M has at least one number belonging to set S?
For the above simple example the answer is
S=[1 5 7]
However, I need a function that can compute the set S for M with any large size.
Accepted Answer
More Answers (1)
Walter Roberson
on 22 Aug 2018
a = {'(1,3),'
'(5,4),'
'(9,7),(6,8),'
'(7,3),'
'(7,9),(2,4),(3,10),'
'(5,8)' };
nums = cellfun(@str2double, regexp(a, '\d+', 'match'), 'uniform', 0);
nums will now be a cell array of numeric row vectors.
Finding the minimal set would come after that. I think perhaps you might be able to use the sorts of techniques used to find a covering set
For example if you were to take nums and form the matrix
M = [
1 0 1 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 0
0 0 1 0 0 0 1 0 0 0
0 1 1 1 0 0 1 0 1 1
0 0 0 0 1 0 0 1 0 0
]
then the question would be to find the binary x with the lowest sum such that M * x' >= 1 for each row
This could probably be found with integer programming.
16 Comments
Hossein
on 22 Aug 2018
Walter Roberson
on 22 Aug 2018
M = {'(1,3),'
'(5,4),'
'(9,7),(6,8),'
'(7,3),'
'(7,9),(2,4),(3,10),'
'(5,8)' };
nums = cellfun(@str2double, regexp(M, '\d+', 'match'), 'uniform', 0);
The M you posted is not valid MATLAB.
Perhaps your M is a single character vector that has embedded newline characters?
Hossein
on 22 Aug 2018
Walter Roberson
on 22 Aug 2018
Please attach a sample data as a .mat file.
Hossein
on 22 Aug 2018
Walter Roberson
on 22 Aug 2018
Use the save command to save it as a .mat . For example,
save 415738.mat M
Hossein
on 22 Aug 2018
Walter Roberson
on 22 Aug 2018
The regexp() I posted works fine with your M.
>> celldisp(nums(1:20))
ans{1} =
1174 1175
ans{2} =
1149 1162
ans{3} =
1148 1149
ans{4} =
1147 1148
ans{5} =
1145 1146
ans{6} =
1138 1140
ans{7} =
1137 1160
ans{8} =
1134 1136
ans{9} =
1130 1131
ans{10} =
1130 1134 1131 1136 1132 1135
ans{11} =
1128 1129
ans{12} =
1127 1133
ans{13} =
1109 1114
ans{14} =
1108 1109
ans{15} =
1107 1108
ans{16} =
1106 1107
ans{17} =
1104 1105
ans{18} =
1092 1106
ans{19} =
1091 1092
ans{20} =
1090 1093
Hossein
on 22 Aug 2018
Walter Roberson
on 22 Aug 2018
I think you can frame this as a Vertex Cover problem. If I am correct, then use the routine grMinVerCover() from https://www.mathworks.com/matlabcentral/fileexchange/4266-grtheory-graph-theory-toolbox
Hossein
on 22 Aug 2018
Walter Roberson
on 22 Aug 2018
You asked for the minimal set, and solving the Vertex Cover would give you the minimal set. Either the set is one of the minimal ones or it is not, so if it calculates the solution correctly than it cannot be too conservative.
Hossein
on 22 Aug 2018
Walter Roberson
on 22 Aug 2018
You are not setting up the right vertex cover problem.
Create one vertex for each unique value in the list of values. Then, for each row in the M table, create a vertex for it and create an edge from that vertex to each of the nodes mentioned in the row, independent of order. Now run the vertex cover algorithm.
Hossein
on 23 Aug 2018
Walter Roberson
on 23 Aug 2018
+----------------------------------------+
| |
(1)------------------------[1, 2] |---[1,3]
| |
---(2)-------------------------+ +--[2, 3] |
| | | | |
| |---------------------------------+ | |
| | |
| (3)------------------------------------+ |
| | |
| +--------------------------------------------+
+----------------------------------------------------[2,4]
|
(4)-----------------------------------------------+
and so on.
Each node in your table gets connected to each of the nodes in the list of numbers for that node.
Now run the Vertex Cover algorithm. Or the Edge Cover algorithm, whichever turns out to be more appropriate.
Categories
Find more on Profile and Improve Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!