# Create a matrice in a for loop

3 views (last 30 days)
Miguel Albuquerque on 16 Jul 2022
Answered: sai charan sampara on 18 Oct 2023
I have this moving radar, that detects targets in a specific area, identified by pixels(xtarget,ytarget).
In the end I have this distance_matrix, where rows are the distance to the target(R1-R2-Rd) and the columns are the position of the radar, since this radar is moving.
waypoints are the position of the radar, and it has, 400 positions(1:400 pixels). But the distance_matrix I have, saves the distances in 1 x 400, whic is correct, but if I have more than 1 target in each column(i), this code only saves one distance. I wanted that distance_matrix was a matrix that if column (i) had more that one distance to save, saved all the distances along that column. So imagine i=200, and in i there are 3 distances to save, so in i=200, distance_matrix should be 3x1 , and not 1x1 like I have, can you help me?
this image is a example when I run the code:
in waypoint=141, the radar detects target(100,325) and target(150,300), but when I go to distance_matrix, this has only one value of R1+R2-Rd, I want it to have two in this case
distance_matrix=zeros(1,length(waypoints)); % Define distance Signal matrix
R1_matrix=zeros(1,length(waypoints)); % Define distance Signal matrix
Rm_matrix=zeros(1,length(waypoints)); % Define distance Signal matrix
R2_matrix=zeros(1,length(waypoints)); % Define distance Signal matrix
for i=1:numel(waypoints) % For each waypoint
Y_transmitter = waypoints(i);
for xx=1:400
for yy=1:400
if AoI(xx,yy) ~= 0 % Target detection
....
if status ==1 & Pt ==1 & Pr ==1 & Wi_Surv ~=0 & Wi_transmit ~=0 % SAR Passive detection
....
R1=sqrt( (X_transmitter-X_target).^2 + (Y_transmitter-Y_target).^2); % Distance transmitter-target in meters
distance_matrix(:,i)=R1+R2-Rd;
R1_matrix(:,i)=R1;
R2_matrix(:,i)=R2;
Rm_matrix(:,i)=X_transmitter-X_target;
end
end
end
end
end
KALYAN ACHARJYA on 17 Jul 2022
"I wanted that distance_matrix was a matrix that if column (i) had more that one distance to save, saved all the distances along that column."
...
You may look for cell array , where rows are the distance to the target & columns are the position of the radar. In that case you can save multiple data points in same distance & position.
Am I understanding the question correctly?
Miguel Albuquerque on 17 Jul 2022
Edited: Miguel Albuquerque on 17 Jul 2022
I thought of doing this
distance_matrix=cell(2,length(waypoints));
How can I solve this
for ....
distance_matrix(:,i)=R1+R2-Rd;
But i get this error:
Conversion to cell from double is not possible.
And I need to make the calculation in double, for the code that comes next

sai charan sampara on 18 Oct 2023
Hello Miguel,
I understand that you are trying to find a way to create a matrix where each entry can be one or more values based on the number of distances to save.
You can try using cell arrays for this application. A cell array is a data type with indexed data containers called cells, where each cell can contain any type of data. You can use the distance and position of radar as the indexes. Then any number of data points can be added into each cell as an array based on the requirement.
The error “Conversion to cell from double is not possible” is because of incorrect indexing. Using parenthesis to index a cell array returns a cell as output. You are trying to assign a double value to a cell data type which throws an error. Hence when assigning a value to a cell in a cell array use curly brackets (“{}”) to index the cell array. It can be done like the code below.
C = {'2017-08-16',[56 67 78]}
C = 1×2 cell array
{'2017-08-16'} {[56 67 78]}
C(2,:) = {'2017-08-17',[58 69 79]};
C(3,:) = {'2017-08-18',[60 68 81]}
C = 3×2 cell array
{'2017-08-16'} {[56 67 78]} {'2017-08-17'} {[58 69 79]} {'2017-08-18'} {[60 68 81]}
C(1,:)
ans = 1×2 cell array
{'2017-08-16'} {[56 67 78]}
C{1,2} = [55 68 78]
C = 3×2 cell array
{'2017-08-16'} {[55 68 78]} {'2017-08-17'} {[58 69 79]} {'2017-08-18'} {[60 68 81]}
Hope this helps.
Thanks,
Charan.

### Categories

Find more on Detection, Range and Doppler Estimation in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!