How to detect differencens between 2 vectors that are sorted?

1 回表示 (過去 30 日間)
vthuongt
vthuongt 2015 年 8 月 24 日
コメント済み: Bjorn Gustavsson 2015 年 8 月 24 日
I have to following problem: I got a vector A = [1 20 3650 4000 NaN NaN] of fixed size (here 6). Given A I generate B in such a way that I generate a new value depending on the last non-NaN value of A (in this case 4000). The new Value is now 4000*rand. For example 4000*rand = 1111. So B becomes B = [1 20 1111 3650 4000 NaN], also of fixed Size 6 and sorted. More precisely the value 1111 is appended to the vector A and then sorted so that it becomes B.
But if I just have B and A given and the knwoledge of the generation process, how can I detect the 1111? My naive approch would be, looping from 1 to end through all the elements and since I know the vectors are sorted the first missmatch is the new element. But I wondered whether their is a nicer/faster solution for this?
Thanks in advance!
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 8 月 24 日
Please give a correct example, in your example B is 1x5 not 1x6 like you said, and explain how 4000*rand is located in your new matrix?
vthuongt
vthuongt 2015 年 8 月 24 日
Thanks for your remark. I edited the question for clarification.

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

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2015 年 8 月 24 日
Surely this answer is what your teacher want you to learn:
setdiff(B,A)
  2 件のコメント
vthuongt
vthuongt 2015 年 8 月 24 日
編集済み: vthuongt 2015 年 8 月 24 日
Thank you very much. This function seems to be exactly what I needed. I have no teacher :) This problem arouse in another larger program I am working on. But anyways- how does this function performs in contrast to the for-loop? If for example I have much bigger sets?
Bjorn Gustavsson
Bjorn Gustavsson 2015 年 8 月 24 日
I have no idea but just became curious. Please investigate and then tell us about the results.

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

その他の回答 (0 件)

カテゴリ

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