Shifting columns while filling matrix with data

Hi,
I have created a database which is a 5x5 matrix of [1, 2, 3, 4, 5]. the first column consists of all ones, second of twos, etc. I am coding a moving object. At each position of the object, I am calculating 5 points that lay on a line perpendicular to the position of the object. I defined a matrix with these points the same size as my database with all zeros. I want to 'fill' the matrix by assigning a new value from my database in a loop. At the first location of the object the first column of my matrix filled with zeros, is now filled with the first column of my database. when the object has moved one timestep further, the second column of my zeros matrix should be filled with the first column of ones from my database and it's previous position should be updated to the second column of the database, etc. Instead it fills it the same way as my database. Can someone tell me how I can code this?
Thank you in advance!

 Réponse acceptée

From what I understood -
n=5;
%database
x = meshgrid(1:n)
x = 5×5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
%matrix
y = zeros(n);
for k=1:n
y(:,1:k) = x(:,k:-1:1)
end
y = 5×5
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
y = 5×5
2 1 0 0 0 2 1 0 0 0 2 1 0 0 0 2 1 0 0 0 2 1 0 0 0
y = 5×5
3 2 1 0 0 3 2 1 0 0 3 2 1 0 0 3 2 1 0 0 3 2 1 0 0
y = 5×5
4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0
y = 5×5
5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1

8 commentaires

Kyra Oomen
Kyra Oomen le 1 Déc 2023
Modifié(e) : Kyra Oomen le 1 Déc 2023
That is exactly what I mean. Thank you so much!
You're welcome!
I was wondering if you could help met with this: when i plot this, the previous points aren't assigned the new values. To clarify, the data in matrix y is the colordata in a scatter plot. In the scatterplot it always shows a color that corresponds with 1.
I assume that you want to use the matrix generated in each iteration. Is that correct?
Also, do you plot a scatter plot in each iteration?
It would be helpful for me to help you if you could provide a better picture of the logistics.
That is correct.
Here is a snippet of the code, I hope this is helpful and makes my question more clear. Than you so much for you effort and willingness to help!
x_lijn and y_lijn are the location of the points on the line perpendicular to the location of the moving object.
% before the loop
[X] = meshgrid(linspace(1,5,5))
colordata = zeros(size(Bellen));
% Inside the loop
colordata(:,1:i) = X(:,i:-1:1);
hold on;
plot(x_schip, y_schip)
scatter(x_lijn(:,i), y_lijn(:,i), 30, colordata(:,i),'filled')
colorbar;
pause(0.1);
hold off;
Yes, I have a better understanding of your query now, thank you.
The color values are expected to be 3-element vector between 0 and 1, both inclusive, aka RBG triplets, for each set of data plotted.
Here's an example -
%Three sets of data, red, blue and green colors
scatter(rand(10,3), rand(10,3), [], [1 0 0; 0 1 0; 0 0 1], 'filled')
You will have to adjust your data accordingly.
I got it to work. Thank you
Glad to have helped!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by