Take 1D slice between two points in 2D MATLAB array - arrays

I'm trying to write a function that given 2 points in a 2D MATLAB array will return a 1D array of the elements connecting the points.
So arr=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20;21 22 23 24 25 ]
and the points between arr(4,2) and arr(1,5) would be returned as [17 13 9 5].
For example, although I'm fine with points being interpolated if required.
If it helps this is how I do it in Python
length = int(np.hypot(x1-x0, y1-y0))
xx, yy = np.linspace(x0, x1, length), np.linspace(y0, y1, length)
zi = arr[xx.astype(np.int), yy.astype(np.int)]

You could use interp2:
lets say we'd like to sample 20 evenly space points from (1.3, 2.4) to (4.6, 3.5)
arr=[ 1 2 3 4 5;
6 7 8 9 10;
11 12 13 14 15;
16 17 18 19 20;
21 22 23 24 25 ]
[x, y] = meshgrid(1:5,1:5)
xx = linspace (1.3, 4.6, 20)
yy = linspace (2.4, 3.5, 20)
interp2(x,y,arr, xx,yy)
gives:
8.30000000000000
8.76315789473684
9.22631578947368
9.68947368421053
10.15263157894737
10.61578947368421
11.07894736842105
11.54210526315790
12.00526315789474
12.46842105263158
12.93157894736842
13.39473684210526
13.85789473684211
14.32105263157895
14.78421052631579
15.24736842105263
15.71052631578947
16.17368421052632
16.63684210526316
17.10000000000000
You can see how the line follows your mesh:
mesh(x,y,arr)
hold on
plot3(xx,yy,interp2(x,y,arr, xx,yy))

Related

How to swap in 3D array using MATLAB?

Is this the right statement to swap 3D matrix :
A([1 2],:,2)= A([2 1],:,2)
A is defined as a 3D matrix of 3x3x3.
Try to visualize you have 3D matrix of 3x3x3 (a cube type). Now you have to swap the column of front face to column of top face. Is this type of swapping possible in MATLAB?
Yes.
This switchs the 2 top rows in A(:,:,2):
A = reshape(1:27,[3,3,3]);
before = A(:,:,2)
A([1 2],:,2) = A([2 1],:,2);
after = A(:,:,2)
The result:
before =
10 13 16
11 14 17
12 15 18
after =
11 14 17
10 13 16
12 15 18

Matlab: reshape 3-dimensional array into 2-dimensional array

I have a 3x3x2000 array of rotation matrices that I need to transform into a 2000x9 array.
I think I have to use a combination of permute() and reshape(), but I don't get the correct output order.
This is what I need:
First row of 3x3 array needs to be columns 1:3 in the output
Second row of 3x3 array needs to be columns 4:6 in the output
Third row of 3x3 array needs to be columns 7:9 in the output
I have tried all possible combinations of numbers 1 2 3 in the following code:
out1 = permute(input, [2 3 1]);
out2 = reshape(out1, [2000 9]);
But I always end up with the wrong order. Any tips for a Matlab newbie?
How about a simple for-loop?
for i=1:size(myinput,3)
myoutput(i,:)=[myinput(1,:,i) myinput(2,:,i) myinput(3,:,i)];
% or
% myoutput(i,:)=reshape(myinput(:,:,i),[],9);
end
It's not simple as using permute and reshape, but it is transparent and easier for debugging. Once everything in your program runs perfectly, you can consider to rewrite such for-loops in your code...
You had a mix-up in your permute
a = reshape(1:9*6, 3, 3, []);
a is a 3x3x6 matrix, each
a(:,:,i) = 9*(i-1) + [1 4 7
2 5 8
3 6 9];
So
out1 = permute(a, [3,1,2]);
out2 = reshape(out1, [], 9);
Or in one line
out3 = reshape(permute(a, [3,1,2]), [], 9);
So
out2 = out3 =
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54

how to take diffrence between cell array and simple integer array in matlab?

i have extracted 23 sentences from a text file which are divided and shown in separate line each sentence is given a number in ascending order {1,2,3,...}, code i used for this is as follows:
sentences = regexp(F,'\S.*?[\.\!\?]','match')
char(sentences)
now i did some processing and got filtered answer which shows a subset of sentences as shown below:
result = 1 4 5 9 11 14 16 17
the code i used for result is as follows:
result = unique([OccursTogether{:}]);
display(result)
now what i want to do is to show the sentences that are not present in the result variable for example the result i need is as follows:
result2 = 2 3 6 7 8 10 12 13 15 18 19 20 21 22 23
remember sentences is [1*N] cell where as result is simple array saving integers.
The function you are looking for is setdiff:
%// Create an array containing the indices of all the sentences
AllSentences = 1:23;
%// Indices of sentences present
result = [1 4 5 9 11 14 16 17]
%// And not present
NotPresent = setdiff(AllSentences,result)
NotPresent =
Columns 1 through 13
2 3 6 7 8 10 12 13 15 18 19 20 21
Columns 14 through 15
22 23
I'm not sure to understand what is a cell array and what is not, but for cell arrays you can convert them to numeric arrays using cell2mat and apply the same methodology.
Eg:
AllSentences = {1:23};
NotPresent = setdiff(cell2mat(AllSentences),result)

how do I combine only the vectors - Matlab

How do I combine all the vectors in small subsets of vectors in Matlab?
a= [5 6 7] b = [8 9 10] c=[11 12 13] d=[14 15 16] e=[17 18 19]
a combine with b and c:
Outcome:
M1= [ 5 6 7 8 9 10 11 12 13]
a with b and d:
M2 = [5 6 7 8 9 10 14 15 16]
and so on .....
This answer covers the case for an arbitrary number of vectors. The vectors are assumed to be row vectors of equal length.
Let your example data be defined as
a = [5 6 7]; b = [8 9 10]; c = [11 12 13]; d = [14 15 16]; e = [17 18 19];
vectors = {a, b, c, d, e}; %// cell array with any number of row vectors of equal size
n = 3; %// desired subset size
Then: generate all combinations of indices, use that to index into vectors, concatenate into one big row vector, and reshape that to obtain the desired result:
combs = nchoosek(1:numel(vectors), n);
result = reshape([vectors{combs.'}], numel(vectors{1})*n, []).';
This gives a matrix whose first row is your M1, second row is M2 etc:
result =
5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 14 15 16
5 6 7 8 9 10 17 18 19
5 6 7 11 12 13 14 15 16
5 6 7 11 12 13 17 18 19
5 6 7 14 15 16 17 18 19
8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 17 18 19
8 9 10 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19
You can use the cat function:
res = cat(2,a,b,c);
or simply the [] syntax:
res = [a b c];
In both cases, res will contain [5 6 7 8 9 10 11 12 13].
Best,
Use nchoosek.
a = [5 6 7]; b = [8 9]; c = [11 12 13]; d = [14 15 16]; e = [17 18 19];
N = 3;
x = {a,b,c,d,e};
y = nchoosek(x,N);
And you have all the combination of your arrays taken N at a time in a cell array. Each row i of the cell x is a combination, so to have it back as a row vector just do
horzcat(y{i,:})
Or, if you want to get them all and put them in a cell array of size n_combs
n_combs = size(y,1);
out = cell(0,n_combs);
for i = 1 : n_combs
out{i} = horzcat(1, y{i,:});
end
There is no constraint on the size of the arrays that you want to combine, e.g., you can combine
a = [5 7]; b = [8 9]; c = [11]; d = [20 14 15 16]; e = [17 18 19];
However, if you must put together all the combination in a matrix, then the arrays have to be of the same size. In this case Luis Mendo's answer does the job.
Finally, if repetitions are allowed use nmultichoosek instead of nchoosek.

What is the meaning of the addition at the end of this array declaration?

I'm tasked with implementing an algorithm which was supplied as Matlab (which none of us have any experience with) into our c++ application.
There is an array declared as such:
encrypted = [18 10 20 13 6 25 21 13 17;
2 26 4 29 22 9 5 29 1;
19 11 21 12 7 24 20 12 16;
% ... many rows like this ...
13 21 11 18 25 6 10 18 14]+1;
What is the semantic meaning of the +1 at the end of the array declaration?
Simply adding 1 to each entry:
>> [1 2 3; 4 5 6]
ans =
1 2 3
4 5 6
>> [1 2 3; 4 5 6] + 1
ans =
2 3 4
5 6 7
If you have MATLAB around, you could have figured that out by just trying. If you do not, I hope you have a very clear picture of what the code is doing and write a good test suite, since you won't be able to compare your new code's output to the MATLAB one.
The +1 means that all elements of the written matrix will be increased by one.
Example
out = [1 2;
3 4] + 1;
disp(out)
2 3
4 5

Resources