Need help abt error.
1 view (last 30 days)
Show older comments
Here is my code. till the time the alto_dur and teb_dur are exactly same vectors, then there is no problem but if i make
alto_dur = [ 2*t t 2*t t 5*t]; and
treb_dur=[*t* t 2*t t 5*t]; I get error
??? Error using ==> plus Matrix dimensions must agree.
Error in ==> test at 38 final_song = cellfun(@plus, alto, treb, 'Uniform', 0); % Alto Tones and Treble tones are added
The code is here fs=8500; t=.18; % this t is used in duration as a multiple or single identity % Key Number and duration of each note %************************Alto************ alto_keys = [ 46 46 0 46 0]; alto_dur = [ 2*t t 2*t t 5*t]; %************************************
%******************Treeble*********************************** % It is high or acute part of the musical system
treb_keys=[56 56 0 56 0 ]; % Duration of ech note treb_dur=[2*t t 2*t t 5*t]; %********************************************
alto = cell(1, length(alto_keys)); % It will creat a cell array having one row and % columns equal to number of keys for i = 1:length(alto_keys) % The for loop continues till number of Keys alto{i} = adsr_note(alto_keys(i), alto_dur(i)); % Function adsr_note is called which calculates %tone for each key for the specified duration and apply ADSR Envolop on it. This tone is stored in % alto{i} end
treb = cell(1, length(treb_keys)); % It will creat a cell array having one row and % columns equal to number of keys for k = 1:length(treb_keys) % The for loop continues till number of Keys treb{k} = adsr_note(treb_keys(k), treb_dur(k)); % Function adsr_note is called which calculates %tone for each key for the specified duration and apply ADSR Envolop on it. This tone is stored in % treb{i} end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0); % Alto Tones and Treble tones are added % Option Uniform or UniformOutput set to 0 (or false) means, that output from function will be stored in cell array
% Concatinating Tone Vectors tone = cat(2, final_song{:}); % It will concatinate arrays of final_song in a row. 2 means %row. If we will use 1, it will concatinate in a column
1 Comment
Wayne King
on 28 Sep 2011
Hi, it's hard to read your code above. If the error is occurring at
cellfun(), please give us the sizes of alto and treb
size(alto), size(treb)
Accepted Answer
Wayne King
on 28 Sep 2011
Sorry, I just noticed I hadn't changed something. Here is a quick hack with a for loop. I'm sure there is a more elegant way if I had time. Basically you have to figure out which vectors are longer in alto or treb and then pad the shorter vector.
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur=[ 3*t t 2*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[3*t t 3*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
for kk = 1:length(treb)
if (length(treb{kk})==length(alto{kk}))
alto{kk} = alto{kk};
else
alto{kk} = [alto{kk}, zeros(1,length(treb{kk})-length(alto{kk}))];
end
end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
1 Comment
Wayne King
on 28 Sep 2011
I should note than in this example I knew that treb contained the longest vectors because of your _dur vector.
More Answers (7)
Wayne King
on 28 Sep 2011
Hi, I don't have any problem running this code:
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur = [ 2*t t 2*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[2*t t 2*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
with your adsr_note function. Maybe you have something left over in your workspace that is causing an error? Or maybe you're using a differnt version of adsr_note (perhaps you saved it in a different folder). Can you clear your workspace and try it?
Does
>>which adsr_note
return what you think it should?
Wayne
0 Comments
Wayne King
on 28 Sep 2011
Can you just pad the shorter vectors with zeros to make them equal length with the longer ones. That way the vector addition will be defined.
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur = [ 3*t t 3*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[3*t t 3*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
alto = cellfun(@(x) [x zeros(1,length(treb{1})-length(alto{1}))], alto,'UniformOutput',false);
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur = [ 3*t t 3*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[3*t t 3*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
alto = cellfun(@(x) [x zeros(1,length(treb{1})-length(alto{1}))], alto,'UniformOutput',false);
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!