add another column to the end of matrix

7 visualizaciones (últimos 30 días)
abdul rehman
abdul rehman el 9 de Jul. de 2021
Comentada: KSSV el 9 de Jul. de 2021
I want to add the results of "a" at the end of Carr_veh matrix. I mean in the 6th column the all results of a shouled be. Maybe create another matrix and merge. I am unable to do this.
Code:
x1=randi(5);
y1=randi(5);
time_e=randi([1 5]);
Carr_veh = [rand(100,1),randi([0,1],100,1),randi([5,8],100,1),randi([5,8],100,1),randi([5,12],100,1)]
for i=1:100
a=sqrt(((Carr_veh(i,3))-x1)^2+(Carr_veh(i,4))-y1)^2
end
  1 comentario
Simon Chan
Simon Chan el 9 de Jul. de 2021
Editada: Simon Chan el 9 de Jul. de 2021
a is replacing itself in the loop, try replacing the for loop with the following
a = sqrt((Carr_veh(:,3)-x1).^2 + (Carr_veh(:,4)-y1).^2);
another_matrix = horzcat(Carr_veh,a)

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 9 de Jul. de 2021
Editada: KSSV el 9 de Jul. de 2021
x1=randi(5);
y1=randi(5);
time_e=randi([1 5]);
Carr_veh = [rand(100,1),randi([0,1],100,1),randi([5,8],100,1),randi([5,8],100,1),randi([5,12],100,1)] ;
a = zeros(100,1) ; %<--- Initialize a into a column matrix for appending later
for i=1:100
a(i)=sqrt(((Carr_veh(i,3))-x1)^2+(Carr_veh(i,4))-y1)^2 ;
end
iwant = [Carr_veh a] ; % add a as the last column
Actually you need not to use a loop. Just use:
x1=randi(5);
y1=randi(5);
time_e=randi([1 5]);
Carr_veh = [rand(100,1),randi([0,1],100,1),randi([5,8],100,1),randi([5,8],100,1),randi([5,12],100,1)] ;
a = sqrt(((Carr_veh(:,3))-x1).^2+(Carr_veh(:,4))-y1).^2 ;
iwant = [Carr_veh a] ;
  1 comentario
KSSV
KSSV el 9 de Jul. de 2021
What logical error? Check are hand calculations and code are same or not.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by