Efficient way to index values in an array

9 Ansichten (letzte 30 Tage)
Kao Yue Raymond Cheng
Kao Yue Raymond Cheng am 16 Mai 2020
I am having trouble thinking of an efficient way to index the values in the following array 'pts'.
nbox = 10^4; % Number of boxes
n = nbox^(1/2); % Boxes per row
p = 100;
dist = linspace(1/(2*p),(1-1/(2*p)),10); % 1/2 point spacing from box boundaries
xt = zeros(1,10*n);
width = 1/n;
for i = 1:n
xt(1,(10*(i-1)+1):(10*i)) = width * dist + width*(i-1); % Assign coord values
% Note there are 10 points per 'row of points'.
% 10 rows of these gives us 100 points per box.
end
[A,B] = meshgrid(xt,xt);
pts = [B(:) A(:)]; % Points (x,y) from coord values
For context, I have divided a square into boxes/squares (given by 'nbox' value). I've produced 100 points per box, uniformly distributed (not random!) with a half point spacing from the edges of their respective boxes. Now this is where I'm having trouble thinking of how to code the following indexing problem:
I'm looking to assign all the points in the first box, starting from the bottom left corner of the grid the index value of 1. Then moving to the box to the right, (the second box), all the points in this box will be assigned the index value of 2. i.e. The box number = index value of all points in that box. Repeating this until we hit the end of the first row, ending with box number . The next row above, starting from the left again, will begin with box number . Repeat until we've indexed all points in the number of boxes. I'm looking to store these index values in another array.
Clearly all the points in the same box will have the same index value, and I'm having trouble thinking of another way to index them instead of going through each point individually and assigning them a number. There's a lot of redundancy since every 100 points will have the same index value.
What is an efficient way to do this indexing?
Thanks.

Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by