Need help for addition of cells

Here is portion ofmy code
alto = cell(1, length(keysa));
for i = 1:length(keysa) % The for loop continues till number of Keys
alto{i} = note(keysa(i), dura(i)); function is called
end
treb = cell(1, length(keyst));
for i = 1:length(keyst)
treb{i} = note(keyst(i), durt(i)); % function is called
end
Now what i want is that to make a final cell array in which treb{i} and alto{i} are added and stored one by one i mean
final{1}= treb{1}+alto{1}
final {2}= treb{2} +alto{2}
so final is addition of both two.
I tried this one but gave error
final=cell(1,length(keyst)
for k=1:length(keyst)
final{i}=alto(i)+treb(i);
end

 채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 21일

0 개 추천

final=cell(1,length(keyst))
for k=1:length(keyst)
final{i}=alto{i}+treb{i};
end
Or
final = cellfun(@plus, alta, treb, 'Uniform', 0);

추가 답변 (5개)

moonman
moonman 2011년 9월 21일

0 개 추천

I am getting this answer when i used ur first option
Undefined function or method 'plus' for input arguments of type 'cell'.
Error in ==> exp2 at 53 final{k}=alto{k}+treb(k)+bass{k};

댓글 수: 6

Fangjun Jiang
Fangjun Jiang 2011년 9월 21일
Walter forgot one right parenthesis in the first line. You can add it yourself.
Walter Roberson
Walter Roberson 2011년 9월 21일
The first line needed a ')'. I have edited it in to place.
Walter Roberson
Walter Roberson 2011년 9월 21일
Walter copied the line as-is from the original posting...
moonman
moonman 2011년 9월 21일
Neither is second option is working, the error is
??? Error using ==> plus
Matrix dimensions must agree.
Error in ==> exp2 at 56
final = cellfun(@plus, alto, treb,'Uniform', 0);
moonman
moonman 2011년 9월 21일
Yes i have placed the missin bracket now but error remains same for both options
what can be possibilities
length of treb and alto is same
Walter Roberson
Walter Roberson 2011년 9월 21일
length of treb and alto might be the same, but each of them is a cell array possibly containing a vector or matrix. Do the sizes of those agree?
Try this:
find(cellfun(@(A,T) ~(isequal(size(A),size(T)) || numel(A) == 1 || numel(T) == 1), alto, treb))
If any numbers are printed out, then they are each indices K such that alto{K} cannot be added to treb{K} because the sizes are different.

댓글을 달려면 로그인하십시오.

moonman
moonman 2011년 9월 21일

0 개 추천

Can anybody help me for this solution

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 9월 21일
I am doing multiple things at the same time; I can't always notice and test and write a response within 8 minutes. :(

댓글을 달려면 로그인하십시오.

moonman
moonman 2011년 9월 22일

0 개 추천

Ok Walter Thanks for halping , when i use ur command, i get this answer
298 299 301 302 303 304 306 307 308 309 310 311 312
What does this mean
there are 313 cells in each alto and treb and length ofalto andtreb is same? howto overcome this problem
moonman
moonman 2011년 9월 22일

0 개 추천

Ok now i have removed those cells which were creating error
now with the command
find(cellfun(@(A,T) ~(isequal(size(A),size(T)) numel(A) == 1 numel(T) == 1), alto, treb))
i get
ans =
Empty matrix: 1-by-0
Now the command
final = cellfun(@plus, alto, treb, 'Uniform', 0);
is also executing
but when i use finally this command
soundsc(final,fs)
it gives error
??? Undefined function or method 'min' for input arguments of type 'cell'.
Error in ==> soundsc at 27 xmin = min(x(:));
*whereas if i do
soundsc(final{3},fs)
or soundsc(final{68},fs)
it works
individually*

댓글 수: 2

moonman
moonman 2011년 9월 22일
the command at 27 are
alto = cell(1, length(keysa));
for i = 1:length(keysa)
alto{i} = note(keysa(i), dura(i)); %function id called
end
Walter Roberson
Walter Roberson 2011년 9월 22일
Shrug. You never did indicate what you want the additions to mean, so I just followed your outline, which is not going to produce a single array suitable for using in soundsc()
If you are attempting to do additive synthesis of sound, then you need to define the time relationships between the various components.
If it happens that the alto and treb of each pair are to be played at the same time, but only one pair at a given time, and no pause between adjacent pairs, then you want
fullsound = horzcat(final{:});
and then soundsc(fullsound,fs);
If you get a dimension error when you try that, change the horzcat() to vertcat().

댓글을 달려면 로그인하십시오.

moonman
moonman 2011년 9월 22일

0 개 추천

Thanks a lot Walter for ur constant help the problem is solved i forgot to concatinate
by adding this my problem is solved and music is being played by addition of alto and treeble
tone = cat(2, final{:});
thanks a lot Walter Once again u are great

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by