For element 12 in 2nd column, I need to know the corresponding element (neighbor/ adjacent) in column 1 (1,2,3 in column 1 is adjacent to 12). I tried INTERSECT which gives me row number where they intersect. From that information, I need to search element in 1st column. Is there any direct way to do this? Thanks.
MATLAB: Call adjacent element in row
neighborrow index
Related Solutions
You could do this:
w = floor(sqrt(30))rows = 1 : wcounter = 1;for k = 1 : length(rows) columns(k) = floor(30/rows(k)); for col = 1 : columns(k) fprintf('For %d rows, and %d columns, there are %d elements.\n',... rows(k), col, rows(k) * col); allMatrices{counter} = rand(rows(k), col); counter = counter + 1; endend
You'll get every possible matrix size with 30 or fewer elements in it. And each one will be a cell in the allMatrices cell array. It has to be a cell array rather than a 3-D matrix because the matrices are not all the same size. You'll see in the command window:
w =
5
rows =
1 2 3 4 5
For 1 rows, and 1 columns, there are 1 elements.
For 1 rows, and 2 columns, there are 2 elements.
For 1 rows, and 3 columns, there are 3 elements.
For 1 rows, and 4 columns, there are 4 elements.
For 1 rows, and 5 columns, there are 5 elements.
For 1 rows, and 6 columns, there are 6 elements.
For 1 rows, and 7 columns, there are 7 elements.
For 1 rows, and 8 columns, there are 8 elements.
For 1 rows, and 9 columns, there are 9 elements.
For 1 rows, and 10 columns, there are 10 elements.
For 1 rows, and 11 columns, there are 11 elements.
For 1 rows, and 12 columns, there are 12 elements.
For 1 rows, and 13 columns, there are 13 elements.
For 1 rows, and 14 columns, there are 14 elements.
For 1 rows, and 15 columns, there are 15 elements.
For 1 rows, and 16 columns, there are 16 elements.
For 1 rows, and 17 columns, there are 17 elements.
For 1 rows, and 18 columns, there are 18 elements.
For 1 rows, and 19 columns, there are 19 elements.
For 1 rows, and 20 columns, there are 20 elements.
For 1 rows, and 21 columns, there are 21 elements.
For 1 rows, and 22 columns, there are 22 elements.
For 1 rows, and 23 columns, there are 23 elements.
For 1 rows, and 24 columns, there are 24 elements.
For 1 rows, and 25 columns, there are 25 elements.
For 1 rows, and 26 columns, there are 26 elements.
For 1 rows, and 27 columns, there are 27 elements.
For 1 rows, and 28 columns, there are 28 elements.
For 1 rows, and 29 columns, there are 29 elements.
For 1 rows, and 30 columns, there are 30 elements.
For 2 rows, and 1 columns, there are 2 elements.
For 2 rows, and 2 columns, there are 4 elements.
For 2 rows, and 3 columns, there are 6 elements.
For 2 rows, and 4 columns, there are 8 elements.
For 2 rows, and 5 columns, there are 10 elements.
For 2 rows, and 6 columns, there are 12 elements.
For 2 rows, and 7 columns, there are 14 elements.
For 2 rows, and 8 columns, there are 16 elements.
For 2 rows, and 9 columns, there are 18 elements.
For 2 rows, and 10 columns, there are 20 elements.
For 2 rows, and 11 columns, there are 22 elements.
For 2 rows, and 12 columns, there are 24 elements.
For 2 rows, and 13 columns, there are 26 elements.
For 2 rows, and 14 columns, there are 28 elements.
For 2 rows, and 15 columns, there are 30 elements.
For 3 rows, and 1 columns, there are 3 elements.
For 3 rows, and 2 columns, there are 6 elements.
For 3 rows, and 3 columns, there are 9 elements.
For 3 rows, and 4 columns, there are 12 elements.
For 3 rows, and 5 columns, there are 15 elements.
For 3 rows, and 6 columns, there are 18 elements.
For 3 rows, and 7 columns, there are 21 elements.
For 3 rows, and 8 columns, there are 24 elements.
For 3 rows, and 9 columns, there are 27 elements.
For 3 rows, and 10 columns, there are 30 elements.
For 4 rows, and 1 columns, there are 4 elements.
For 4 rows, and 2 columns, there are 8 elements.
For 4 rows, and 3 columns, there are 12 elements.
For 4 rows, and 4 columns, there are 16 elements.
For 4 rows, and 5 columns, there are 20 elements.
For 4 rows, and 6 columns, there are 24 elements.
For 4 rows, and 7 columns, there are 28 elements.
For 5 rows, and 1 columns, there are 5 elements.
For 5 rows, and 2 columns, there are 10 elements.
For 5 rows, and 3 columns, there are 15 elements.
For 5 rows, and 4 columns, there are 20 elements.
For 5 rows, and 5 columns, there are 25 elements.
For 5 rows, and 6 columns, there are 30 elements.
You may refer to the piece of code below that uses the find function to achieve your goal without using a loop.
function y = transformAdjArrayOnes(a) y = a; %copying the input array
loc = find(a==1); %finding locations of 1
y(loc+1) = 1; %making right adjacent element to 1
y(loc-1) = 1; %making left adjacent element to 1
end
You may call the above function with your sample array as follows:
>> a = [ 0 0 1 0 0 0 1 1 1 0 0 0 1 0 ]a = 0 0 1 0 0 0 1 1 1 0 0 0 1 0>> transformAdjArrayOnes(a)ans = 0 1 1 1 0 1 1 1 1 1 0 1 1 1
Best Answer