Holding the Previous Value of For Loop and Sum

Hello,
I am trying to plot the values of two operations and I have two nested for loops. For every value of k, the clutter location changes and I want to calculate the conv_clutter for the new k value. Some part of the code is like that.
for k = 1:clutternumber
clutter_x = xa1(k);
clutter_y = ya1(k);
clutter_location = [clutter_x clutter_y ];
for i = 1:1:1024
target_location = ...........
radar_location = ............
conv_target = ..
conv_clutter = ..
sum = conv_target + conv_clutter;
data(i,:) = sum;
end
end
imagesc(real(abs(data)), 'XData', [range]), colormap turbo;
I cannot hold the previous value of the conv_clutter in imagesc. Can someone help me about the algorithm? It just shows me the value that comes from the k = 1 and the others are constant.

 Akzeptierte Antwort

Jon
Jon am 16 Mai 2022

0 Stimmen

The problem is that you overwrite your value of data each time the inner loop is executed.
If you just want to plot each image and not do anything else with it, you could move your line that plots the data inside of the loop and make a new figure for each plot, so something like this:
for k = 1:clutternumber
clutter_x = xa1(k);
clutter_y = ya1(k);
clutter_location = [clutter_x clutter_y ];
for i = 1:1:1024
target_location = ...........
radar_location = ............
conv_target = ..
conv_clutter = ..
sum = conv_target + conv_clutter;
data(i,:) = sum;
end
% plot the image
figure
imagesc(real(abs(data)), 'XData', [range]), colormap turbo;
end
If you want to do more with the data, then you will need to save it, for example in an array with three dimensions, the first dimension could be the clutternumber, and the other two would provide the image, or you could make a cell array of images

5 Kommentare

I don't want to make a new figure sir, I want figure, to show me the sum for all values of k.
I'm sorry, but I'm not clear from what you have written on what you actually want to compute.
It looks like each iteration of the inner loop (i) makes a new row in your image. So when i reaches 1024 you have an image corresponding to the kth clutter number. Is that correct?
Then do you want to make a single image in which the images for each clutter number are summed pixelwise? If so you could do it like this:
numRows = 1024; % number of image rows
numColumns = ... % put in how many columns in your image
data = zeros(numRows,numColumns); % preallocate array to hold image
for k = 1:clutternumber
clutter_x = xa1(k);
clutter_y = ya1(k);
clutter_location = [clutter_x clutter_y ];
for i = 1:1:1024
target_location = ...........
radar_location = ............
conv_target = ..
conv_clutter = ..
sum = conv_target + conv_clutter;
data(i,:) = data(i,:) + sum; % sum the entries to make composite image
end
end
% plot the image
figure
imagesc(real(abs(data)), 'XData', [range]), colormap turbo;
I just want to compute the range between my radar and clutter. But every time k increases, the computation of new clutter starts. So if I have 8 clutter targets, I want to compute for k=1:1:8. The problem is I can not hold the previous value on the image. After k=1, imagesc does not show me the result of k=2,3,4,5.... It does not change and it is stationary. I expect to see the hyperbola shape but I just see it in the k=1 not in 2,3,4,5. Hope that is clear.
Jon
Jon am 18 Mai 2022
Sorry, I am not familiar enough with your problem domain to understand what you are saying. If you could express it more generically, explaining what mathematical operations you are performing, what your "image" is and what exactly you want saved I might be able to help further. Otherwise the general advice I would give you is that if you do not want a variable in the inner loop to keep overwriting something then you need to save it with a variable that includes some kind of index from the outer loop.
I solve it sir, your thoughts are right. I just want to overwrite it but the way that i use is not true. data(i,:) = data(i,:) + sum; solves my problem. I was writing like data(i,:) = convolve_rx + convolve_rx_clutter + sum;
Thank you so much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by