Arithmetic in Cell Arrays

7 vues (au cours des 30 derniers jours)
CX
CX le 11 Nov 2013
Commenté : Walter Roberson le 12 Nov 2013
I have the following variables in Cell arrays. b_x{k}, x{k}, c{k}.
I want to perform the following operation within the cell array.
D{k} = sqrt((b{k} - x{k}).^2 + (c{k}).^2)
When doing this I realize that these arithmetic operations cannot be done through a cell array. So do I have to do it using generalized operations such as gadd, gsubtract etc? If yes then for the element wise square should I use 'gmultiply' twice? Is there no other way?
Thanks

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Nov 2013
D = cellfun( @(B, X, C) sqrt((B - X).^2 + C.^2), b, c, x);
  2 commentaires
CX
CX le 11 Nov 2013
Why is there B, X, C and b, c, x? Please explain?
Walter Roberson
Walter Roberson le 12 Nov 2013
Your b, c, and x are your cell arrays. cellfun is going to step through corresponding locations in the cell arrays, b{5} with c{5}, x{5} for example, and pass those values as arguments to the anonymous function of three arguments @(B, X, C) sqrt((B - X).^2 + C.^2) . The B and X and C received in the anonymous functions are specific values extracted from the cell array, rather than the cell arrays as a whole. If I had used the variable names b, x, and c within the anonymous function, you would have gotten confused between the cell arrays as a whole and individual values extracted from the arrays.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 11 Nov 2013
Try using cell2mat(), or just avoid using cell arrays and use numerical arrays in the first place. Why do you need cell arrays? Are you storing mixed data, such as numbers with an associated string?
  3 commentaires
Image Analyst
Image Analyst le 11 Nov 2013
That is not a reason for using cell arrays. You can use regular arrays for b, x, and c, though if they have different lengths in each folder then D will have to be a cell array. Please give an example of them for two different folders.
CX
CX le 11 Nov 2013
I will try the procedure you suggested and get back to you
Thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by