# Projection problem as a for loop

3 vues (au cours des 30 derniers jours)
ISABELLE GUNDRUM le 16 Jan 2023
Déplacé(e) : Matt J le 16 Jan 2023
I am trying to create a loop that finds the mean projection onto the line. The coordinates of the line arfe set, but the coordinate of the vector being projected changes and I need the average projection and standard deviation. I don't have much experience with loops so this is what I have so far. I keep getting an error that says "Error using '-'. Integers can only be combined with integers of the same class, or scalar doubles".
coord_o = [78 132 199];
coord_b = [145 161 184];
vector_b = coord_b - coord_o;
M = size(Ca,1);
for i = 1:M;
coord_a = Ca(i);
vector_a = coord_a(i) - coord_o;
project = dot(vector_a(i),vector_b)/norm(vector_a(i))
disp(mean(project))
end
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Luca Ferro le 16 Jan 2023
where and how is Ca defined?

Connectez-vous pour commenter.

### Réponses (1)

John D'Errico le 16 Jan 2023
Your main problem is that the vector Ca is not composed of double precision numbers. That causes the code to fial, because the further computations you are doing with those numbers involve things that will fail for what are very probably uint8 integers.
So in your for loop, change this:
coord_a = Ca(i);
to this:
coord_a = double(Ca(i));
As well, make sure that coord_o and coord_b are also doubles. Even though you show them here as doubles by default, we do not know they are doubles in your code as you are using them.
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
ISABELLE GUNDRUM le 16 Jan 2023
Déplacé(e) : Matt J le 16 Jan 2023
Ca is a matrix of many 3D coordinates. I want it to subtract coord_o from each coordinate in the matrix as individual vectors.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by