How to compare multiple strings and assign values to the comparison?

5 ビュー (過去 30 日間)
German Preciat Gonzalez
German Preciat Gonzalez 2016 年 6 月 28 日
コメント済み: José-Luis 2016 年 6 月 28 日
I have data from 6 different sources in a form of strings. I want to compare which strings are the same.
So if I have:
d1='111'
d2='223'
d3='111'
d4='125'
d5='111'
d6='125'
d1,d3,d5 are the same, d4 and d6 are the same too and d2 is different to all, so I will like to receive a vector where similar strings have the same number, like this:
[1 2 1 4 1 4]
if all the strings are different I will like to receive something like this:
[1 2 3 4 5 6]
or all the same
[1 1 1 1 1 1]
so far I've use a very ugly program with a lot of IF and ISEQUAL, any suggestions I will really appreciate it!

採用された回答

José-Luis
José-Luis 2016 年 6 月 28 日
編集済み: José-Luis 2016 年 6 月 28 日
Copying beginning from Siva because I'm lazy:
K = [{'111'} ; {'223'} ; {'111'} ; {'125'} ;{'111'} ; {'125'}] ;
Solving your problem more efficiently:
[val,ia,ib] = unique(K,'stable') ; % get unique values
your_answer = ia(ib);
  3 件のコメント
José-Luis
José-Luis 2016 年 6 月 28 日
My pleasure.
Guillaume
Guillaume 2016 年 6 月 28 日
I find that method of creating a cell array very odd!
K = {'111'; '223'; '111'; '125'; '111'; '125'};
is less to type and a lot simpler.

サインインしてコメントする。

その他の回答 (3 件)

Guillaume
Guillaume 2016 年 6 月 28 日
Dr. Siva Srinivas Kolukula has the right idea: use unique. His answer is unnecessarily complicated though.
First though, do not use numbered variables. If they're numbered it means that they've got some sort of relationship. That relationship is much better expressed by putting the content of these variables together into the same container: matrix, cell array, map, table, etc. In your case, a cell array seems best.
Anyway, the third return value of unique is what you're after:
d = {'111', '223', '111', '125', '111', '125'}
[~, ~, uid] = unique(d, 'stable') %'stable' is optional. If not present unique ids are assigned alphabetically
  3 件のコメント
Guillaume
Guillaume 2016 年 6 月 28 日
Well, the question was "I will like to receive a vector where similar strings have the same number", which the above does.
José-Luis
José-Luis 2016 年 6 月 28 日
Indeed, but that's not what the example result showed. Anyway, interpreting questions is an art sometimes...

サインインしてコメントする。


KSSV
KSSV 2016 年 6 月 28 日
編集済み: KSSV 2016 年 6 月 28 日
clc; clear all ;
K = [{'111'} ; {'223'} ; {'111'} ; {'125'} ;{'111'} ; {'125'}] ;
[val,idx] = unique(K,'stable') ; % get unique values
iwant = zeros(size(K)) ; % initialize what you want
count = 0 ;
% loop for each equal string
for i = 1:length(idx)
count = count+1 ;
Index = strfind(K,val{i}) ;
Index = find(not(cellfun('isempty', Index))) ;
iwant(Index) = count ;
end

KSSV
KSSV 2016 年 6 月 28 日
Jose-Luis idea is good....but only one step is needed.
[val,ia,your_answer] = unique(K,'stable') ; % get unique values
  1 件のコメント
José-Luis
José-Luis 2016 年 6 月 28 日
No, what the op wanted, if I understood correctly, is the index to the first occurrence. Therefore that wouldn't work.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by