Unable to iterate my array even though both sides appear to be the same sides.
조회 수: 1 (최근 30일)
이전 댓글 표시
Hey Im trying to create a series of of notes to play in sucessive order. Every time this runs I get the following error.
"Unable to perform assignment because the left and
right sides have a different number of elements.
Error in DSPLab4>createMusic (line 88)
maxtimes(i) = max(Time{1,i});"
However when I use size(maxtimes) and size(Time) they both are 1x4 so I am not sure why they arent the same size. The code is below:
note_vec = [76, 79, 81, 83];
d_vec = [0,5, 0.5, 0.5, 0.5];
start_vec = [0, 0.5, 1, 1.5];
Test = createMusic(d_vec, note_vec, fs, start_vec);
function music = createMusic(d_vect,note_vect,fs,start_vect)
%the arguments
%d_vect the vector of note durations
%note_vect the vector of note numbers
%fs the sampling frequency
%start_vect the vector of starting times eg [1,0,0] means the first note
%starts after 1 second, while the second and third notes start at 0sec.
for k = 1:1:length(note_vect)
[note,time_note] = createNote(d_vect(k),note_vect(k),fs,start_vect(k));
A{k}=note;
Time{k}=time_note;
end
%check the end of each signal
maxtimes = zeros(1,length(note_vect));
for i=1:length(note_vect)
maxtimes(i) = max(Time{1,i});
end
%append zeros only if necessary to time align the signals
maximus = max(maxtimes);
for i=1:length(note_vect)
A{1,i} = [A{1,i};zeros(ceil(maximus)*fs-ceil(maxtimes(i))*fs,1)];
end
%convert cell to matrix
AlmostThere = zeros(ceil(maximus)*fs,length(note_vect));
for i=1:length(note_vect)
AlmostThere(:,i) = A{1,i};
end
%sum columns of the matrix to get the final music
music = sum(AlmostThere,2);
end
댓글 수: 2
답변 (1개)
Florian Bidaud
2023년 11월 21일
편집: Florian Bidaud
2023년 11월 21일
Following your comment, you have the answer,
Time{1} is a 0x1 double, so max(Time{1,1}) is empty, therefore you cannot assign it to maxtimes(1)
The issue comes from the function createNote for k = 1
댓글 수: 3
Florian Bidaud
2023년 11월 21일
Oops sorry I meant 1, was coding in Python just before answering the question :D
I'm correcting the answer now
Dyuman Joshi
2023년 11월 25일
@Aidan Marrinan, The solution is to include a Fail-safe in your code.
for i=1:length(note_vect)
z = max(Time{1,i});
%Fail safe
if isempty(z)
z=NaN; %Or 0, or any other value you'd like to use as a placeholder
end
maxtimes(i) = z;
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!