How to compare two lists
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
i have two lists lets say a=[1 2 3 ] and b =[1 2 3 4 5 ] and i want to compare each element of ' a ' with all elements of "b" it means that 1 is compared with whole list "b" then 2 is compared then 3 and if the element of "a" is member of "b" then output "a"else 0.
採用された回答
Walter Roberson
2018 年 3 月 27 日
a .* ismember(a, b)
19 件のコメント
Umer Khitab
2018 年 3 月 27 日
thanks for your intrest.will you please help me a little bit more here if the number is common in both lists then the output is that number if not then output is 0 Will you please tell me how to change the output from 0 to some other value lets say if 2 is not common in both lists then the output is 2+1 or 2-1 or any other operation.Thanks in advance
Birdman
2018 年 3 月 27 日
@Umer: you want to output zero if a is not a member of b, but this answer does not do that.
Umer Khitab
2018 年 3 月 27 日
@ birdman ;Thanks for your time but this answer is giving 0 if the number is not not a member of both
Walter Roberson
2018 年 3 月 27 日
"Umer: you want to output zero if a is not a member of b, but this answer does not do that."
Huh?
>> b =[1 2 3 4 5 ]
b =
1 2 3 4 5
>> a = [1 2 7]
a =
1 2 7
>> a .* ismember(a,b)
ans =
1 2 0
Walter Roberson
2018 年 3 月 27 日
mask = ~ismember(a, b); c = a;
then
c(mask) = 2; %change the missing items to the fixed value 2
or
c(mask) = a(mask) - 1; %change the missing items to one less than their original value
Umer Khitab
2018 年 3 月 28 日
編集済み: Walter Roberson
2018 年 3 月 28 日
hi @walter thanks for your time and intrest
The above code is working good but I need little bit more help now if i want to compare each element of ' a ' with all elements of "b" it means that 1 is compared with whole list "b" then 9 is compared then 2 and if the element of "a" is member of "b" then output "a" else the last element of 'b'.
lets say a=[1 9 2 4 ]
b=[1 2 3 4 5 6 7 8 10 12 ]
output must be 1 in first case but in second case i.e for 9 output should be 8
Walter Roberson
2018 年 3 月 28 日
編集済み: Walter Roberson
2018 年 3 月 28 日
mask = ~ismember(a, b);
c = a;
c(mask) = b(end);
I suspect you might be asking for the last value in b that does not exceed the value in a ? If so then
sb = [-inf, sort(b), inf];
[~, bin] = histc(a, sb);
c = sb(bin);
The behavior if the element of a was smaller than any element of b was not defined, so I have arbitrarily defined it here as being to output -inf
Umer Khitab
2018 年 3 月 29 日
Thanks alot @walter almost my problem is solved.Thank you Now the last problem Will you please tell how to transform these values to a audio file . I means i have 2560 points and i want to get audio file from these values and these point will act as an amplitude...
Walter Roberson
2018 年 3 月 29 日
Assuming you have a vector c, but since I do not know the range of values or data types involved:
if isa(c, 'uint8') || isa(c, 'int16') || isa(c, 'int32')
scaled_c = c;
elseif isa(c, 'uint16') & all(c <= intmax('int16'))
scaled_c = int16(c * 2 - intmax('int16'));
elseif isa(c, 'uint32') & all(c <= intmax('int32'))
scaled_c = int32(c * 2 - intmax('int32'));
elseif all(c >= 0)
scaled_c = double(c) ./ double(max(c)) * 2 - 1;
elseif all(c >= -1 & c <= 1)
scaled_c = double(c);
else
cmax = double(max(c));
cmin = double(min(c));
scaled_c = (double(c) - cmin) ./ (cmax - cmin) * 2 - 1;
end
Now audiowrite scaled_c with appropriate filename and sampling frequency.
Notes about the conversion I do above:
- if the datatypes are uint8, int16, or int32, then the code assumes you knew what you were doing with the range of values and leaves them completely unchanged
- if the range of values is -1 to +1 then the code assumes you knew what you were doing with the range of values and leaves them unchanged, but alters the datatype to double. The cases where the data type could end up altered are single() that are in the right range, and int64(). The way the code is set up cannot affect the other integer data types
- if the values are all non-negative, then the code assumes you knew what you were doing about the lower end of the values, and rescales [0 max(c)] to [-1 +1] . So, for example, your range of values happened to be 30000 to 50000 then this will be mapped first to [30000/50000, 50000/50000] and then the [0 1] range would be mapped to [-1 +1] so the [30000/50000, 50000/50000] would end up mapping to [1/5 1]. It would also have been entirely reasonable to instead map [min(c) max(c)] to [-1 +1]: in the absence of information from you about what should be done, a decision had to be made
- otherwise, in the case where there was a mix of positive and negative values but the range was outside [-1 +1], the code scales [min(c) max(c)] to [-1 +1]
For any particular case where you know the range and the data type in advance, then the code can be rewritten to at most three lines.
Umer Khitab
2018 年 3 月 31 日
My range of data values are from 0 to 40.And data type is integer
Walter Roberson
2018 年 3 月 31 日
Okay, and do you want 0 to correspond to minimum amplitude and 40 to correspond to maximum amplitude? If so then
scaled_c = double(c)./20 - 1;
Umer Khitab
2018 年 4 月 1 日
編集済み: Walter Roberson
2018 年 4 月 1 日
0 to correspond to minimum amplitude and 40 to correspond to maximum amplitude it is right but why we required to do the scaling why not we directly put them to the audio file ? direct write c to the audio file?
Walter Roberson
2018 年 4 月 1 日
If your c is uint8, it would be possible for you to audiowrite() the data directly. However, the uint8(40) would be then be interpreted as only 40/255 of maximum amplitude.
audiowrite() does not take max() of the data you supply and mark that as being the maximum amplitude to make everything else relative to: if it did that then it would not be possible to write sound files with different volumes.
Umer Khitab
2018 年 4 月 2 日
hi @walter Scaled_c have values between -1 to 1 but there is no sound in the audio file I am using the sampling frequency=25 rest each and everything is ok audio file is created but there is sound in the file..will you please help me solving this issue Thanks alot
Walter Roberson
2018 年 4 月 2 日
How long is your scaled_c ?
Using a sampling frequency of 25 is borderline. Humans can barely hear down to 20 Hz, and even then it usually has to be loud to hear it.
Umer Khitab
2018 年 4 月 2 日
The audio file is about 5sec long Scaled_c have 128 points
Umer Khitab
2018 年 4 月 5 日
Hi WALTER I am facing a problem I have 2 different arrays both of same length,but the amplitudes are different.but after doing scaling and setting sampling frequency to 256hz audio file is created but there is different between two files.Will you please help me in this ? Thanks
Walter Roberson
2018 年 4 月 5 日
With that test (and the speakers I was using) I could only hear a couple of pops in the mid 20's of Hz, and could not clearly hear a sound until around 33 Hz. Without headphones I would probably not be able to hear your 128 point 5 second sound, and possibly not even with headphones either. I would recommend you at least double your sampling frequency. (But 256 Hz should be fine.)
In what respect are the two files different?
Jason
2018 年 5 月 8 日
Greetings, I’ve a similar question, but I'm trying to create a third matrix based on matching pairs of numbers in two other matrices. Current example:
a = [17,9;23,3;24,3;23,3;24,3;9,6;10,6;23,3;24,3;23,3]
b = [17,1;18,1;21,1;23,3;24,3;23,4;24,4;9,6;10,6;14,12;23,12;24,12;23,14]
My goal is to create 'c' where both columns in 'a' and 'b' are the same. So, row 1 of 'a' would be excluded, as would the first 2 rows of 'b', etc. etc.. Any help with accomplishing this task would be greatly appreciated.
Cheers, Jason
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Audio Processing Algorithm Design についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
