random close numbers within an array

how can i randomly extract a number and one of its four closest 'neighbours' out of an array 100x100?

5 Comments

This will give you a random element within the matrix A:
A = rand(100);
r = randi([1 size(A,1)]); % random row
c = randi([1 size(A,2)]); % random column
A(r,c)
By 'one of its four closest neighbours' do you mean any of the four adjacent elements? Or the adjacent element which is closest in value? If the former, you could always just pick
if r+1 <= size(A,1)
A(r+1,c)
else
A(r-1,c)
end
imagine this matrix
[1 2 3 4;5 6 7 8;9 10 11 12]
if the randomly chosen number in the first step is, for example, 7, how can i randomly choose a number between the numbers 3,6,8 and 11 (the neighbours for number 7)?
What if your randomly selected number lies on an edge of the array, or at a corner? Now will you randomly select from only 2 or 3 neighbors, or do something different?
How about if you make it easy and just pick a random row or column between 2 and 99.
x = 1 + randi(98, 1);
y = 1 + randi(98, 1);
Will that work for you? Then you can always be assured that a neighbor will be in the array. If not, then you've got a lot more checking to do to make sure the neighbor is not outside the array. Lots of ways but they all involve lots of if blocks or loops.
yes, thats the idea. For border conditions then there will be less neighbours, so i randomly have to choose between 2 or 3 whether they are on the edge or at a corner.

Sign in to comment.

 Accepted Answer

I would choose a random integer from 1 to 10000. We can use that as a linear index into the elements of your array, as if the array were unrolled into a vector. Since there are 100*100=10000 elements in total, this works.
Next, whatever the element is, which elements are adjacent to it? Thus up, down, right, left? For a 100x100 array, how would we find them? The trick is to understand how MATLAB stores elements in an array, and how that unrolled index works.
I'll do this for a fully general sized array, to make it more clear how things work. So assume an array of size nr by nc, thus nr rows and nc columns.
nr = 100;
nc = 100;
offset = [1 -1 nr -nr]; % down, up, right, left
% choose a random element in the matrix
randelem = randi(nr*nc,1);
% what are the neighbors of this element?
neighbor = randelem + offset;
% which of those neighors fall off the edge of the world?
[r,c] = ind2sub([nr,nc],neighbor);
neighbor((r < 1) | (c < 1) | (r > nr) | (c > nc)) = [];
% a random choice among one of those that remain
neighbor = neighbor(randi(numel(neighbor),1));
% convert back to row and column indices:
[r,c] = ind2sub([nr,nc],[randelem,neighbor]);
r
r =
55 56
c
c =
81 81

1 Comment

Catarina
Catarina on 6 Apr 2020
Edited: Catarina on 6 Apr 2020
thank you! I just have one more question, what exactly does ind2sub do?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 5 Apr 2020

Edited:

on 6 Apr 2020

Community Treasure Hunt

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

Start Hunting!