フィルターのクリア

maintaining relative positions of each elements of the resulting array after applying Union()

1 回表示 (過去 30 日間)
Hi,
I have a question regarding Matlab's Union.
Suppose I have the following two string arrays.
x1 = ["assets"; "fixed asset"; "total assets"; "short-term bank loans"; "revenue"; "equity"];
x2 = ["assets"; "fixed asset"; "total assets"; "long-term bank loans"; "revenue"; "equity"];
If I do this
x3 = union(x1,x2,'stable')
Then the result is
x3 = ["assets"; "fixed asset"; "total assets"; "short-term bank loans"; "revenue"; "equity"; "long-term bank loans"]; % result
But what I want is
x3 = ["assets"; "fixed asset"; "total assets"; "short-term bank loans"; "long-term bank loans"; "revenue"; "equity"]; % what I want
Any help or suggestions are appreciated. Thank you very much.
Aditya
PS This is not a homework problem.
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 12 月 29 日
union is a set operation so if duplicates appear later then they would have to be eliminated .I suspect that is not what you want .
II suspect what you want is "Compare corresponding pairs of elements .if they are the same use one copy . If they are different use the first then the second ."
Is that more accurate ? If it is then it can be done efficiently with some logical indexing.
Aditya Tan
Aditya Tan 2018 年 12 月 29 日
Hi Walter,
I suppose yes. I had thought this operation can be done efficiently with Union. The resulting elements from the Union() operation are correct--it's just their positions that are not.
Then, are you suggesting to achieve this with a loop instead? Thanks.
Aditya

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 12 月 29 日
x3 = [x1.'; x2.'];
mask = [true(1, length(x1)); x3(1,:) ~= x3(2,:)];
x3 = x3(mask);
  2 件のコメント
Aditya Tan
Aditya Tan 2019 年 1 月 3 日
Hi Walter,
Thanks for your help last week. I actually oversimplified my problem and still couldn't figure it out. Perhaps, if you have time, please take a look at the following thread:
Thanks. Aditya

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Aerospace Blockset についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by