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
"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
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...
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
