assign cell arrays in an array

조회 수: 2 (최근 30일)
Evangelos Xenos
Evangelos Xenos 2022년 11월 18일
답변: Bala Tripura Bodapati 2022년 11월 21일
Hello everyone,
I have this script of code, part of a bigger program, and I need to save the jammer and eavs position values [#, #, #] in a list/array every time my NCS_r is greater than the assigned threshold. How do I input these values into an array? I have tried amending them in ALL_jammer_pos[] and ALL_eavs_pos, but didn't work (it's in comments the relevant lines of code).
here is the code:
Rt= ##;
ALL_jammer_pos= [];
ALL_eavs_pos= [];
for ii=1:1:10000
...
for iii=1:1:1000
jammer_posx = randi([-500 500],1,1);
%fprintf('%d', jammer_posx);
jammer_posy = randi([-500 500],1,1);
eavs_posx = randi([-500 500],1,1);
eavs_posy = randi([-500 500],1,1);
jammer= [jammer_posx jammer_posy H];
eavs= [eavs_posx eavs_posy H];
...
NCS_r= ###;
% if NCS_r >= Rt
% ALL_jammer_pos= [ALL_jammer_pos jammer];
% ALL_eavs_pos= [ALL_eavs_pos eavs];
% end
end
The jammer and eavs positions come in the format of a 1x3 double, where 1st value is the X-axis value, 2nd the Y-axis value and 3rd the Z-axis value. Z-axis value is fixed as the Height operating in, which is not crucial to be kept necessarily.
Do you have any ideas how I can put all the jammer and eavs position values in relative arrays/lists to keep record of the positions used?

답변 (1개)

Bala Tripura Bodapati
Bala Tripura Bodapati 2022년 11월 21일
Hi Evangelos
It is my understanding that you would like to iteratively append the 'jammer' and 'evas' positions to an array when a specified condition is satisfied.
As the dimension of the variables 'jammer' and 'evas' is [1x3], a possible workaround is to use 'cell arrays' to store the value of the variables in every iteration whenever the specified condition is satisfied.
The following code illustrates the use of cell arrays as mentioned above:
Rt= 30;
ALL_jammer_pos= {};
ALL_eavs_pos= {};
for ii=1:1:10000
for iii=1:1:1000
jammer_posx = randi([-500 500],1,1);
%fprintf('%d', jammer_posx);
jammer_posy = randi([-500 500],1,1);
eavs_posx = randi([-500 500],1,1);
eavs_posy = randi([-500 500],1,1);
H=30;
jammer= [jammer_posx jammer_posy H];
eavs= [eavs_posx eavs_posy H];
NCS_r= 40;
if NCS_r >= Rt
ALL_jammer_pos{end+1}= jammer;
ALL_eavs_pos{end+1}= eavs;
end
end
end
Refer the cell array documentation for information on using cell arrays.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by