Look up value between two matrix - arrays

I assume that I have a 2D matrix that range from 0 to 255 (size 2x3) and a 1D matrix (size 256x1). In which, 1D matrix stores the information of 256 pixel values of 2D array. For example,
2D matrix is
[0 1 2
255 2 2]
and 1D matrix
[0 0 0.1 ....0.5]
I want to make a new 2D matrix that store the information of 1D matrix at each pixel value. We can see 2 in the 2D matrix has value is 0.1 and 255 has information values is 0.5. So I want to make a matrix such as
[0 0 0.1
0.5 0.1 0.1]
How to make that matrix by MATLAB code?

You need to index into the 1D array with the elements of the 2D array as indices. Now, MATLAB's indexing starts from 1 and you have values in the interval [0,255] in your 2D indexing array, so you need to add 1 to them before indexing. Thus, assuming array1Dand array2D as the variable names for the 1D and 2D arrays respectively, use this -
array1D(array2D+1)
Example
Let us assume some values for these two arrays as demo, shall we? Let array2D has 6 pixel values from 0 to 5 (to simulate 0 to 255 in your case) and array1D has 6 elements for each of the 6 pixels. The inputs and the code run -
array2D = [
0 1 2 ;
5 2 2]
array1D = [105 103 107 102 108 101]
out = array1D(array2D+1)
out =
105 103 107
101 107 107
Let's do the verification.
Now, array1D(1,1) was 0 and therefore out(1,1) must be the first element from array1D i.e. 105, is it? hell yeah it is!
Similarly, array1D(1,2) was 1 and thus, out(1,2) must be the second element from array1D i.e. 103, is it? YES!
...
array1D(2,1) was 5 and thus, out(2,1) must be the final element from array1D i.e. 101, is it? YES!
... check the rest of the output elements for yourself?

Related

Using one array values to access another array elements Matlab

I am doing some 3D and 4D matrix manipulation in Matlab.
I have created a 2D array which row values contain the index values of interest in a 3D matrix.
Assuming array A of size (Nx2)
A=[2 3 1;5 6 2;7 9 3;3 3 4;1 5 5]
2 3 1
5 6 2
7 9 3
3 3 4
1 5 5
Then, I want to use these elements to manipulate matrix B of size (NxMxL)
B=rand(9,9,5);
So I want to set B(2,3,1)=0 which corresponds to A(1,:).
If I naively go B(A(1,:))=0 this doesn't return me the desired output.
What I understand is that Matlab translate this into B=B(:) which reshape the matrix into a 1xNML
and then returns me the elements 2, 3 and 1 of the reshaped matrix.
How can I avoid this and make it understand my argument B(A(1,:))=B(2,3,1)?
use sub2ind , for example zeroing all the elements in B using of the rows in A as indices:
B(sub2ind(size(B),A(:,1),A(:,2),A(:,3)))=0;

Matlab: Creating a blockwise permutation

I have a vector from 1 to 40 and want to shuffle it in such a way that each block of four integers (ten blocks in total) are shuffled only with themselves.
For example: 3 4 2 1 | 7 6 5 8 | 9 11 10 12 | ...
My original idea was to append ten permutation vectors to eachother and then add a 1 to 40 vector to the big permutation vector, but it didn't work at all as expected and was logically wrong.
Has anyone an idea how to solve this?
data = 10:10:120; % input: values to be permuted
group_size = 4; % input: group size
D = reshape(data, group_size, []); % step 1
[~, ind] = sort(rand(size(D)), 1); % step 2
result = D(bsxfun(#plus, ind, (0:size(D,2)-1)*group_size)); % step 3
result = result(:).'; % step 4
Example result:
result =
20 10 30 40 60 50 70 80 110 100 120 90
How it works
Reshape the data vector into a matrix D, such that each group is a column. This is done with reshape.
Generate a matrix, ind, where each column contains the indices of a permutation of the corresponding column of D. This is done generating independent, uniform random values (rand), sorting each column, and getting the indices of the sorting (second output of sort).
Apply ind as column indices into D. This requires converting to linear indices, which can be done with bsxfun (or with sub2ind, but that's usually slower).
Reshape back into a vector.
You can use A = A(randperm(length(A))) to shuffle an array.
Example in Octave:
for i = 1:4:40
v(i:i+3) = v(i:i+3)(randperm(4));
end

How is an array sliced?

I have some sample code where the array is sliced as follows:
A = X(:,2:300)
What does this mean about the slice of the array?
: stands for 'all' if used by itself and 2:300 gives an array of integers from 2 to 300 with a spacing of 1 (1 is implicit) in MATLAB. 2:300 is the same as 2:1:300 and you can even use any spacing you wish, for example 2:37:300 (result: [2 39 76 113 150 187 224 261 298]) to generate equally spaced numbers.
Your statement says - select every row of the matrix A and columns 2 to 300. Suggested reading

convert several 2 dimensional data into 3 dimensional

I would like to combine several 2-D data into 3 dimensional data. I have 124 2-D data (151 x 151) now I want to combine all these data into 3 dimensional, so that it will be like this 124 x 151 x 151. The 2-D data contains NaN elements.
To concatenate multiple 2D arrays into a 3D array, use the generalized concatenation function cat, specifying dimension 3.
For example, given 2D arrays A1,A2,... of equal size:
M = cat(3,A1,A2,...)
Say you have k 2D arrays organized in a cell array, C, where each cell is a 2D matrix, all of size M-by-N:
M = cat(3,C{:});
Then M will be of size M-by-N-by-k. Now if you want to go from M-by-N-by-k, to k-by-M-by-N, use permute or shiftdim:
Mn = permute(M,[3 1 2]); % my preference
Mn = shiftdim(M,2);
NOTE: An alternative to cat is cell2mat:
M = cell2mat(reshape(C,1,1,[]))

change_size_with_no_change_in_info

How to increase the size of a matrix. I have a matrix of size 36 rows and 3000 columns. I want to convert it to an array of size 200 rows and 3600 columns without affecting the information contained in the original matrix.
In any programming language you will need to create the new array and do copy of the previous elements in the new array.
If you are using java i would prefer to use Arraylist.
You can always create 2D array using arrayList in java.Something like this
ArrayList<ArrayList<Type>>
But in C/C++ you will need to do entire copy operation.
There is a way to do this using zero padding.
Assuming matrix A has p rows and q columns and you want to convert it to a matrix B having m rows and n columns (m>p and n>q). You can use
B = padarray(A,[m-p n-q],0,'post');
B will contain the matrix A and the rest of the matrix will be all zeros.
Example:
a=randi(10,[2 2]);
a =
10 10
2 9
b = padarray(a,[4-2 3-2],0,'post');
b =
10 10 0
2 9 0
0 0 0
0 0 0

Resources