Reputation: 34900
Let some variable is string abac
, for example:
un =
{
[1,1] = abac
}
I need to transform it to just abc
, i.e. remove 3d symbol a
.
I have no ideas how to make it easy whithout great perversion with string octave functions. Any ideas?
Upvotes: 0
Views: 188
Reputation: 13081
If you want the unique characters in the order they appear on the original string, the answer is still to unique
but you need its second argument which returns the first (or last) index of each element.
octave> charv = "aaccbbahbc"
charv = aaccbbahbc
octave> [~, i] = unique (charv, "first")
i =
1 5 3 8
octave> charv(sort (i))
ans = acbh
or in a single line:
octave> charv(sort (nthargout (2, @unique, charv, "first")))
ans = acbh
If you have many strings in a cell array, it gets a bit more complicated:
octave> charcv = {"aaccbbahbc", "erfyergfas"}
charcv =
{
[1,1] = aaccbbahbc
[1,2] = erfyergfas
}
octave> [~, i] = cellfun (@unique, charcv , {"first"}, "UniformOutput", false)
i =
{
[1,1] =
1 5 3 8
[1,2] =
9 1 3 7 2 10 4
}
octave> i = cellfun (@sort, i, "UniformOutput", false)
i =
{
[1,1] =
1 3 5 8
[1,2] =
1 2 3 4 7 9 10
}
octave> cellfun (@subsref, charcv, num2cell (struct ("type", "()", "subs", num2cell (i))), "UniformOutput", false)
ans =
{
[1,1] = acbh
[1,2] = erfygas
}
Upvotes: 1
Reputation: 16791
To find unique symbols in a single string within a cell array, use unique
:
un =
{
[1,1] = abac
[1,2] = asdlfkjasdf
}
>> unique(un{1})
ans = abc
>> unique(un{2})
ans = adfjkls
To find the unique elements of all of the strings at once, use cellfun
in conjunction with unique
:
>> cellfun(@unique,un,'UniformOutput',false)
ans =
{
[1,1] = abc
[1,2] = adfjkls
}
Note that by default unique
sorts the elements of the result, and unlike Matlab, Octave does not have a stable
option to turn off this behavior.
Upvotes: 3