How to get the order indices in a character array?

19 Ansichten (letzte 30 Tage)
Akbar
Akbar am 6 Jul. 2018
Kommentiert: Akbar am 7 Jul. 2018
Here is my cell Array, consider it as an input:
ans =
7×1 cell array
{'x_air' }
{'v_air' }
{'p_headspace' }
{'p_environment' }
{'x_air_standard' }
{'N_StirrerSpeed [s^-1]'}
{'v_air;Sparger' }
And I have this character Array (consider as output):
ans =
'v_air,p_headspace,p_environment,x_air,x_air_standard,N_StirrerSpeed [s^-1],v_air;Sparger'
Wanted result:
indices = [2;3;4;1;5;6;7];
The idea is to know which Output is connected to which Input. How to do that?
Once the indices have been recorded, the Input Array will be renamed, but Connection are not changed. For ex.: if i rename 'x_air' at Input then i want to know that it goes to 4th Output.
  1 Kommentar
Ben Frankel
Ben Frankel am 6 Jul. 2018
Do you know that the 'x_air', ... will be unique and won't contain any ','s?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kelly Kearney
Kelly Kearney am 6 Jul. 2018
c = {...
'x_air'
'v_air'
'p_headspace'
'p_environment'
'x_air_standard'
'N_StirrerSpeed [s^-1]'
'v_air;Sparger'};
str = 'v_air,p_headspace,p_environment,x_air,x_air_standard,N_StirrerSpeed [s^-1],v_air;Sparger';
[~,loc] = ismember(regexp(str,',','split'), c);
Results:
loc =
2 3 4 1 5 6 7
As Ben indicates in his comment above, this will only work if none of your target strings include commas. If they do, a little extra parsing will be needed.

Weitere Antworten (2)

Geoff Hayes
Geoff Hayes am 6 Jul. 2018
Akbar - if it is safe to assume that all elements in your string are separated by commas, you could use cellfun to consider each string and find its index in the original array. For example,
myData = {
'x_air'
'v_air'
'p_headspace'
'p_environment'
'x_air_standard'
'N_StirrerSpeed [s^-1]'
'v_air;Sparger'};
myStr = 'v_air,p_headspace,p_environment,x_air,x_air_standard,N_StirrerSpeed [s^-1],v_air;Sparger';
myStrSplit = strsplit(myStr,',')';
myStrIndices = cell2mat(cellfun(@(k)find(strcmp(myData,k) == 1), myStrSplit, 'UniformOutput',false));
We split the string, myStr, on the comma which will create a cell array of strings. We then iterate over each of these strings and look for it in the myData cell array. Since strcmp returns a one/true for a match, then we use find to return the index of that match. The myStrIndices will then be the desired indices of your output to input.

Ben Frankel
Ben Frankel am 6 Jul. 2018
Bearbeitet: Ben Frankel am 6 Jul. 2018
You can do this in two steps. First, replace the strings with their indices in the output string:
s = [the_output, ','];
for ii = 1:numel(the_input)
s = replace(s, [the_input{ii}, ','], [int2str(ii), ',']);
end
Next you can interpret the character array `s = '2,3,4,1,5,6,7,'` as num and take the transpose if you want a column vector:
indices = str2num(s).';

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by