Shifting columns while filling matrix with data

17 views (last 30 days)
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!

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 1 Dec 2023
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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!