cell array expansion
Mostra commenti meno recenti
I have just been bitten by some careless coding, but I am surprised that MATLAB lets me do it and that mlint didn't provide a warning. Why does MATLAB let you do this:
x = {1,2};
y = x{:};
what I wanted to do (and I am sure there are a number of other ways of doing it) was
y = [x{:}];
I can see the advantage of cell array expansion for referencing and parameter passing. For example,
z = magic(5);
z(x{:})
xy = {1:10, 0:9};
plot(xy{:});
Is there any reason for
y = x{:};
to be valid. I feel like it should return an error about the RHS returning more arguments than the LHS.
Risposta accettata
Più risposte (2)
Sean de Wolski
il 21 Lug 2011
One reason: For a function that takes varargin you can feed it all contents of a cell as a separate input.
x = {1,2,3,4}
cat(3,x{:})
Yes, it frustrates me some times too!
3 Commenti
Daniel Shub
il 21 Lug 2011
Sean de Wolski
il 21 Lug 2011
I disagree; that functionality is quite useful. What if the cell contains different sized elements? Concatenating them as you have done will fail.
Daniel Shub
il 21 Lug 2011
Fangjun Jiang
il 21 Lug 2011
Maybe one way to explain it is to treat it as the variable output argument. Like,
MaxValue=max(1:10);
[MaxValue,Index]=max(1:10);
You can do:
x={1,2};
y=x{:}
[a b]=x{:}
Categorie
Scopri di più su Whos in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!