[Syntaxe] Supprimer les itérations
Bonjour,
Je suis relativement débutant en Matlab,et j'ai des habitudes de programmeur non vectoriel... Je voulais juste avoir votre avis sur ce code, et notamment s'il y a la possibilité de gérer les deux boucles vectoriellement, plutôt qu'iterativement.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
function [distance_matrix] = BS_user_distance(antenna_positions, user_positions)
% This function computes distances in between users and all the base
% stations in the BS_positions vector. It uses the standard module
% definition of distance. The result is a cell tab representing the
% distance between all users and all base stations. Each cell has a nested
% vector representing the distance in between a user and all BSs. For
% example, distance_matrix{2,2} will be a vector of antenna_positions length
% containing distances detween user number 2 affiliated to cell2 and all
% base stations.
[line_dim, col_dim] = size(antenna_positions)
% Is the antenna_positions a vector ?
if ((line_dim ~= 1) && (col_dim ~= 1))
error('BS_user_distance:argChk', 'The antenna positions must be a 1D vector')
end
% Is the user_positions a 2D array? We can't have more than 2D.
if (ndims(user_positions) ~= 2)
error('BS_user_distance:argChk', 'The user positions must be a 2D array')
end
% Put the vector in vertical shape if horizontal
if (line_dim == 1)
antenna_positions = antenna_positions.';
end
[line_dim, col_dim] = size(user_positions);
% foreach user in te user_positions matrix, compute the distance between
% him and all BSs and store in the cell array of line index user and column
% index affiliated station.
for (user_line_index = 1:1:line_dim)
for (user_col_index = 1:1:col_dim)
% compute euclidian distance
distance_matrix{user_line_index,user_col_index} = abs( ...
antenna_positions - user_positions(user_line_index,user_col_index));
end
end |
Pour indication, les deux tableaux d'entrée sont complexes.
J'essaie de prendre de bonnes habitudes dès le départ.
Merci.