如何在MATLAB中将不同长度的向量插入矩阵?

时间:2016-05-23 22:08:43

标签: matrix vector

如何将不同长度的向量插入矩阵?例如,我有一个随机定位的节点= 10;之后我发现谁是这个节点的邻居,例如节点:i = 2个邻居是节点:j = 5和6,所以包含这个值的向量是neighb_i = [neighb_i j];但也许我必须添加一些零来填充向量,直到矩阵的长度(在这种情况下节点是10,所以矩阵将是10x10)但我想保留矩阵中所有节点的邻居的所有值,因为当我在下一次交互后使用向量时,这个值正在替换自己,所以在下一次迭代时,当i = 3时,我没有关于何时i = 2的信息。我怎样才能将它存储在矩阵中,长度节点x节点?

close all
clear all
clc

x = 2000;  %m

y = 2000;  %m

nodes = 8; 




%% Random location and direction, calculating the distance;

loc_x = x*rand(1,nodes)

loc_y = y*rand(1,nodes)

loc = [loc_x' loc_y']

dist(loc_x);

dist(loc_y);

distance = sqrt(dist(loc_x).^2 + dist(loc_y).^2)   % = dist(loc')

distance(1:nodes+1:nodes^2) = inf;  % replace zero diagonal with infinity 


%% Power:



noise_power_dBm = -90; %dBm 

noise_power_w = 10^((noise_power_dBm - 30)/10); % W

% The channel is based on a path-loss model in which  is distance between    nodes i and j 
% and 3 - is attenuation exponent considerd as 3 ( 1:6 ) 

channel_gain = (1)./(distance).^3;   %dB

channel_gain(1:nodes+1:nodes^2) = inf;  % replace zero diagonal with infinity


min_SNR_dB = 10; %dB, minimum required SNR at the receiving nodes

min_SNR = 10^(min_SNR_dB/10);

p_max_dBm = 10;   % dBm

p_max_w = 10^((p_max_dBm - 30)/10);  % W


p_uni = (min_SNR*noise_power_w)./(channel_gain); % unicast power between two nodes

p_uni(1:nodes+1:nodes^2) = inf;  % replace zero diagonal with infinity 

plot(loc_x,loc_y,'r*'); 



change = 1;

cost = inf(1,nodes)   % at the beginning cost of all nodes are infinity 

cost(1) = 0;          % except the cost of node 1 = 0 ( source node ) 

while change == 1   % change is when a child chooses parent node and change its parent node; if its a =1 its go again in for loops 

change = 0;     % when i am coming inside while loop I change the change parameter to 0 and if some changes occurse 


for i = 2:nodes %i - child node   first i = 2, then j is 1,2,3...8 it checks all the nodes starts from 1 because source node can be a neighbour of other node

neighb_i = [];

for j = 1:nodes  % j - parent node  

if p_uni(i,j) < p_max_w 

   neighb_i = [neighb_i j];  %Found who are the neighbours  ; to use a matrix not a vector ????????

end
% calculate the cost for every nodes First Dijkstra, later MC 
% and choose the parent node for every node ; if we have some changes
% ---> change = 1

for k = [neighb_i j] % neighb_i

% cost(i,k) = 1:nodes;


end
end
keyboard
end
% At the end of this while - I have to know what is the parent node for
% every node and when I come out I can draw a graph.
end


%%
gplot(nodes,loc)

%plot(source,'b*');
plot(loc_x,loc_y,'rO');
axis([x y x y]);

hold on;
grid on;
grid on, xlabel('x'), ylabel('y');
title('Random nodes');

0 个答案:

没有答案