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
Related
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))
This question already has an answer here:
Matlab - Transpose a 3D matrix only in the third dimension
(1 answer)
Closed 5 years ago.
I am trying to figure out how to import large array of data into 3D matrix to a specific order. I have already asked two question but i have not get reliable answer yet and get down voted too. Since then i have done some work and was able to import data to 3D matrix using reshape function. Instead of shooting actual problem, this is a simulation of actual problem.
k=1:27 % create a array of 27 data
r=reshape(k,[3,3,3]) % convert the array into 3 x 3 x 3 matrix,
The results of the first page and second of the matrix is, the data is placed along the columns, but i wanted to place them along rows, The transpose function does not work with ND arrays, I tried to use permute but i did not get the desired result, One solution will be perform transpose to each page, but that will break the 3D matrix in to many 2D matrices.
r(:,:,1) =
1 4 7
2 5 8
3 6 9
r(:,:,2) =
10 13 16
11 14 17
12 15 18
the expected outcome should be,
r(:,:,1) =
1 2 3
4 5 6
7 8 9
Link to the actual problem is,
Thanks
Is this what you want?
result = permute(r, [2 1 3]);
This permutes the first two dimensions. For your example r,
>> k = 1:27;
>> r = reshape(k, [3,3,3]);
>> result = permute(r, [2 1 3]);
>> result
result(:,:,1) =
1 2 3
4 5 6
7 8 9
result(:,:,2) =
10 11 12
13 14 15
16 17 18
result(:,:,3) =
19 20 21
22 23 24
25 26 27
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
I have an array in MATLAB
For example
a = 1:100;
I want to select the first 4 element in every successive 10 elements.
In this example I want to b will be
b = [1,2,3,4,11,12,13,14, ...]
can I do it without for loop?
I read in the internet that i can select the element for each step:
b = a(1:10:end);
but this is not working for me.
Can you help me?
With reshape
%// reshaping your matrix to nx10 so that it has successive 10 elements in each row
temp = reshape(a,10,[]).'; %//'
%// taking first 4 columns and reshaping them back to a row vector
b = reshape(temp(:,1:4).',1,[]); %//'
Sample Run for smaller size (although this works for your actual dimensions)
a = 1:20;
>> b
b =
1 2 3 4 11 12 13 14
To vectorize the operation you must generate the indices you wish to extract:
a = 1:100;
b = a(reshape(bsxfun(#plus,(1:4)',0:10:length(a)-1),[],1));
Let's break down how this works. First, the bsxfun function. This performs a function, here it is addition (#plus) on each element of a vector. Since you want elements 1:4 we make this one dimension and the other dimension increases by tens. this will lead a Nx4 matrix where N is the number of groups of 4 we wish to extract.
The reshape function simply vectorizes this matrix so that we can use it to index the vector a. To better understand this line, try taking a look at the output of each function.
Sample Output:
>> b = a(reshape(bsxfun(#plus,(1:4)',0:10:length(a)-1),[],1))
b =
Columns 1 through 19
1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43
Columns 20 through 38
44 51 52 53 54 61 62 63 64 71 72 73 74 81 82 83 84 91 92
Columns 39 through 40
93 94
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)