In an assignment A(I) = B, the number of elements in B and I must be the same. (For Loop)
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello, I am getting the error in the title with this code
EDU>> for i=1:6
note(i)=[3,2,1,3,2,1];
xtemp=[];
x=[xtemp note(i)];
end
Outside for loop note = [ sin wave, sin wave, sin wave here] replace sin wave with an actual sin wave which is my code.
I do not understand what is going on. i goes from 1-6 so it has 6 elements and i put 6 numbers inside note(i).
Help would be appreciated.
1 Kommentar
Antworten (2)
Star Strider
am 10 Aug. 2012
Bearbeitet: Star Strider
am 10 Aug. 2012
I'm not quite sure what you want to do, so I'll take a guess.
One problem with the code you posted is that:
note(i)=[3,2,1,3,2,1];
assigns a vector of six values to a single scalar element of vector note. That won't work.
In these lines in your loop:
xtemp=[];
x = [xtemp note(i)];
the first line will reset xtemp to an empty matrix on every iteration of the loop. In the second line, variable x will continuously overwrite its previous value and will (because of the first line) concatanate note(i) with an empty xtemp instead of the previous values of xtemp.
With respect to your loop, I suggest these changes:
xtemp=[];
for ki=1:6
note(:,ki) = [3,2,1,3,2,1]';
xtemp = [xtemp note(:,ki)];
end
When you concatanate vectors to form matrices, be sure your vectors are the orientation (as column or row vectors) you want them. In my code here, I specified that note is a matrix of column vectors. I'm not quite sure what you are doing, but this seems to match with the code you posted. If you want a 1 x 36 vector of repeating [3,2,1,3,2,1] vectors, I refer you to the repmat function.
It's also worth mentioning that xtemp will be a [6 x 21] matrix, while note (in the code I suggested) will be a [6 x 6] matrix.
19 Kommentare
Star Strider
am 12 Sep. 2012
Bearbeitet: Star Strider
am 12 Sep. 2012
You have to write your note15 function to accept note and b8 as inputs.
I decided to write an example function for you to experiment with.
Try this example code:
Notes = [3 3 10 10 12 12 10 8 8 7 7 5 5 3]';
Durns = [1; 1; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 1; 2]*0.35;
Song = [];
for k1 = 1:12
Song = [Song; Note(Notes(k1),Durns(k1)); zeros(250,1)];
end
soundsc(Song, 8192)
with this function:
function y = Note(Frq,Dur)
% FRQ is an integer referencing a particular frequency in the ‘Freq’ array
% DUR is the duration of each note in seconds
% ©StarStrider 2012 09 12
Ampl = [0.346; 0.0628; 0.0616; 0.1073; 0.0802];
Mult = [1:5]';
Freq = 220 * 2.^([0:11]/12);
Note = zeros(1,length([1:Dur*8192]));
for k2 = 1:5
NoteV = Ampl(k2) .* sin(2*pi*Mult(k2)*Freq(Frq)*[1:Dur*8192]/8192);
Note = Note + NoteV;
end
y = Note';
return
First, copy the function to a blank script and save it to the same directory you have the rest of your music files in. Experimnent with it to see how it works. It produces the notes you used earlier (that correspond to notes 4, 6, and 8 in my function), as well as the rest of the scale. It requires a function m-file because it is too complicated for an anonymous function.
It worked well for me, and sounds good with the little song I tried. You will have to write your own routine to fill in whatever pauses you want between notes, and of course their durations. My code should get you started.
Azzi Abdelmalek
am 19 Aug. 2012
Bearbeitet: Azzi Abdelmalek
am 12 Sep. 2012
maybe you want do this
xtemp=[];
for i=1:6
note(i,:)=[3,2,1,3,2,1];
xtemp=[xtemp note(i)];
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!