# How to subtract contents of cells in cell array?

8 vues (au cours des 30 derniers jours)
lil brain le 27 Fév 2022
Commenté : lil brain le 28 Fév 2022
Hi,
I am trying to get the euclidean distances from xyz coordinates (columns 1-3 of baskets_xyz cells) to a reference point (column 10 in each cell in baskets_xyz). I want to perform the euclidean distance calculation to each cell in baskets_xyz so that I receive a single column for each cell as an output. I have tried the following code:
H = @(x) sqrt(((x(:,1)-x(:,10)).^2) ...
+ ((x(:,2)-x(:,10)).^2) ...
+ ((x(:,3)-x(:,10)).^2));
When I run this code I get the error:
Undefined function 'minus' for input arguments of type 'cell'.
Error in Glitch_comparison_distances (line 96)
H = @(x) sqrt(((x(:,1)-x(:,10)).^2) ...
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

per isakson le 28 Fév 2022
Modifié(e) : per isakson le 28 Fév 2022
The problem is that baskets_xyz is a cell array of cell arrays. Your cellfun statements requires a cell array of double (numeric). There is (R2018b) an obsolete function, flatten, which can be used to convert to a cell array of double.
The code below does the trick without flatten.
% 1×19 cell array
% Columns 1 through 3
% {1617×10 cell} {846×10 cell} {3812×10 cell}
%%
cell_of_double = cellfun( @(c) cell2mat(c), baskets_xyz, 'uni',false );
H = @(x) sqrt(((x(:,1)-x(:,10)).^2) ...
+ ((x(:,2)-x(:,10)).^2) ...
+ ((x(:,3)-x(:,10)).^2));
baskets_xyz_h_ref = cellfun( H, cell_of_double, 'uni',false );
% 1×19 cell array
% Columns 1 through 3
% {1617×1 double} {846×1 double} {3812×1 double}
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
lil brain le 28 Fév 2022

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