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.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Mzo. de 2018

0 votos

a .* ismember(a, b)

19 comentarios

Umer Khitab
Umer Khitab el 27 de Mzo. de 2018
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
Birdman el 27 de Mzo. de 2018
@Umer: you want to output zero if a is not a member of b, but this answer does not do that.
Umer Khitab
Umer Khitab el 27 de Mzo. de 2018
@ birdman ;Thanks for your time but this answer is giving 0 if the number is not not a member of both
Walter Roberson
Walter Roberson el 27 de Mzo. de 2018

"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
Walter Roberson el 27 de Mzo. de 2018
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
Umer Khitab el 28 de Mzo. de 2018
Editada: Walter Roberson el 28 de Mzo. de 2018
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
Walter Roberson el 28 de Mzo. de 2018
Editada: Walter Roberson el 28 de Mzo. de 2018
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
Umer Khitab el 29 de Mzo. de 2018
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
Walter Roberson el 29 de Mzo. de 2018
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
Umer Khitab el 31 de Mzo. de 2018
My range of data values are from 0 to 40.And data type is integer
Walter Roberson
Walter Roberson el 31 de Mzo. de 2018
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
Umer Khitab el 1 de Abr. de 2018
Editada: Walter Roberson el 1 de Abr. de 2018
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
Walter Roberson el 1 de Abr. de 2018
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
Umer Khitab el 2 de Abr. de 2018
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
Walter Roberson el 2 de Abr. de 2018
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
Umer Khitab el 2 de Abr. de 2018
The audio file is about 5sec long Scaled_c have 128 points
Umer Khitab
Umer Khitab el 5 de Abr. de 2018
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
Walter Roberson el 5 de Abr. de 2018
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
Jason el 8 de Mayo de 2018
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Audio Processing Algorithm Design en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 27 de Mzo. de 2018

Comentada:

el 8 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by