I have two large arrays which I will illustrate using the following examples.
The first array A is:
[ 1 21;
3 4;
4 12;
5 65 ];
The second array B is:
[ 1 26;
31 56;
4 121;
5 54 ];
I want to obtain the final array C as following:
[ 1 21 26;
4 12 121;
5 65 54];
i.e. use the common elements of first column from A and B to sieve out the rows I want to extract from arrays A and B and generate C.
Use the two-output vesion of ismember:
[ii jj] = ismember(A(:,1), B(:,1));
C = [ A(ii,:) B(jj(ii),2) ];
Note that in the second line ii is a logical index, whereas jj(ii) is a standard (integer) index.
bsxfun approach -
%// Slightly bigger and not-so-straightforward example to avoid any confusion
A =[ 1 21;
3 4;
4 12;
8 10
5 65]
B = [ 1 26;
31 56;
4 121;
5 54
9 99
8 88]
binmat = bsxfun(#eq,A(:,1),B(:,1).');%//'
[v1,v2] = max(binmat,[],2);
C = [A(any(binmat,2),:) B(nonzeros(v1.*v2),2:end)]
Output -
A =
1 21
3 4
4 12
8 10
5 65
B =
1 26
31 56
4 121
5 54
9 99
8 88
C =
1 21 26
4 12 121
8 10 88
5 65 54
Related
Suppose I have a matrix of dimension [4x4], and a vector of [16x1], I need to multiply every 4 element in the vector in one element in the matrix, (instead of multiplying element in row by element in vector), how can I do that using loop ?
For example here below, the results of the first four elements in the resulted vector as shown in the below example, then the same thing for the second, third and fourth rows in the matrix. :
So the results in that example is supposed to be [16x1]
Thank you
Using kron you can use this one-liner:
%A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
%v = [2 2 2 2 0 0 0 0 1 1 1 1 3 3 3 3].';
sum(kron(A,ones(4,4)).'.*v).'/4
I use the kronecker tensor product to "replicate" 4x4 time the A matrice. After that it's pure algebra.
This is just matrix multiplication in disguise... If your tall vector was a matrix of the same size as the matrix shown, where each highlighted block is a row, it's matrix multiplication. We can set this up, then reshape back into a vector.
You can use indexing to turn this into simple matrix multiplication. A question I answered already today details how the below indexing works using bsxfun, then we just reshape at the end:
% Setup
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
v = [2 2 2 2 0 0 0 0 1 1 1 1 3 3 3 3].';
% Matrix mutliplication
r = numel(v)/size(A,1);
b = A * v( bsxfun( #plus, (1:r:numel(v)).', 0:r-1 ) );
% Make result a column vector
b = reshape( b.', [], 1 );
See if this is what you want:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
v = [2 2 2 2 0 0 0 0 1 1 1 1 3 3 3 3].';
r = reshape(sum(bsxfun(#times, permute(A, [3 2 1]), permute(reshape(v, 1, [], size(A,2)), [2 3 1])), 2), [], 1);
which gives
r =
17
17
17
17
41
41
41
41
65
65
65
65
89
89
89
89
There are details that I assumed, but this shoudl do the trick:
A=reshape(1:16,4,4).';
b=repelem([2,0,1,3],1,4).';
c=[];
for row=1:size(A,1)
c=[ c; sum(reshape(repelem(A(row,:),4).*b.',4,[]),2)];
end
I am assuming here that your demo for the vector is just a bad example and that you wont have repeated values, otherwise an easier version can be achieved by just not doing 3/4ths of the multiplications.
If you do not have access to repelem, have a look at alterative codes that do the same thing:Element-wise array replication in Matlab
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 am unable to figure out how to merge two arrays. My data is like this with arrays A and B.
A = [ 0 0; 0 0; 2 2; 2 2;]
B = [ 1 1; 1 1; 3 3; 3 3; 4 4; 4 4; 5 5; 5 5;]
and I need the final array "C" to look like this after merging:
C = [ 0 0; 0 0; 1 1; 1 1; 2 2; 2 2; 3 3; 3 3; 4 4; 4 4; 5 5; 5 5;]
I've tried using different ways with reshaping each array and trying to use a double loop but haven't got it to work yet.
In my actual data it is inserting 9 rows of array B following 3 rows of array A and then repeated 100 times. So, there are 12 new merged rows (3 rows from array A and 9 rows from array B) repeated 100 times with a final row number == 1200. Array A actual data has 300 rows and actual Array B data has 900 rows
thanks,
Here's a solution using only reshape:
A = [ 6 6; 3 3; 5 5; 4 4;]
B = [ 0 0; 21 21; 17 17; 33 33; 29 29; 82 82;]
A_count = 2;
B_count = 3;
w = size(A,2); %// width = number of columns
Ar = reshape(A,A_count,w,[]);
Br = reshape(B,B_count,w,[]);
Cr = [Ar;Br];
C = reshape(Cr,[],w)
The [] in reshape means "how ever many you need to get to the total number of elements". So if we have 12 elements in B and do:
Br = reshape(B,3,2,[]);
We're reshaping B into a 3x2xP 3-dimensional matrix. Since the total number of elements is 12, P = 2 because 12 = 3x2x2.
Output:
A =
6 6
3 3
5 5
4 4
B =
0 0
21 21
17 17
33 33
29 29
82 82
C =
6 6
3 3
0 0
21 21
17 17
5 5
4 4
33 33
29 29
82 82
Approach #1
This could be one approach assuming I got the requirements of the problem right -
%// Inputs
A = [ 6 6; 3 3; 5 5; 4 4;];
B = [ 0 0; 21 21; 17 17; 33 33; 29 29; 82 82;];
%// Parameters that decide at what intervals to "cut" A and B along the rows
A_cutlen = 2; %// Edit this to 3 for the actual data
B_cutlen = 3; %// Edit this to 9 for the actual data
%// Cut A and B along the rows at specified intervals into 3D arrays
A3d = permute(reshape(A,A_cutlen,size(A,1)/A_cutlen,[]),[1 3 2])
B3d = permute(reshape(B,B_cutlen,size(B,1)/B_cutlen,[]),[1 3 2])
%// Vertically concatenate those 3D arrays to get a 3D array
%// version of expected output, C
C3d = [A3d;B3d]
%// Convert the 3D array to a 2D array which is the final output
C_out = reshape(permute(C3d,[1 3 2]),size(C3d,1)*size(C3d,3),[])
Sample run -
A =
6 6
3 3
5 5
4 4
B =
0 0
21 21
17 17
33 33
29 29
82 82
A_cutlen =
2
B_cutlen =
3
C_out =
6 6
3 3
0 0
21 21
17 17
5 5
4 4
33 33
29 29
82 82
Approach #2
Just for the love of bsxfun, here's one approach with it and ones (no reshape or permute) -
%// Assuming A_cutlen and B_cutlen decide cutting intervals for A and B
%// Concatenate A and B along rows
AB = [A;B]
%// Find the row indices corresponding to rows from A and B to be placed
%// according to the problem requirements
idx1 = [1:A_cutlen size(A,1)+[1:B_cutlen]]
idx2 = [A_cutlen*ones(1,A_cutlen) B_cutlen*ones(1,B_cutlen)]
idx = bsxfun(#plus,idx1(:),idx2(:)*[0:size(A,1)/A_cutlen-1])
%// Re-arrange AB based on "idx" for the desired output
C = AB(idx,:)
based on your new criteria this is what you want. My solution isn't the nicest looking (maye someone can think of a nice vectorized approach), but it works
a_step = 2;
b_step = 3;
C = zeros(size([A;B]));
%we use two iterators, one for each matrix, they must be initialized to 1
a_idx = 1;
b_idx = 1;
%this goes through the entire c array doing a_step+b_step rows at a
%time
for c_idx=1:a_step+b_step :size(C,1)-1
%this takes the specified number of rows from a
a_part = A(a_idx:a_idx+a_step-1,:);
%tkaes the specified number of rows from b
b_part = B(b_idx:b_idx+b_step-1,:);
%combines the parts together in the appropriate spot in c
C(c_idx:c_idx + a_step + b_step -1,:) = [a_part;b_part];
%advances the "iterator" on the a and b matricies
a_idx = a_idx + a_step;
b_idx = b_idx + b_step;
end
using
A = [ 6 6; 3 3; 5 5; 4 4;]
B = [ 0 0; 21 21; 17 17; 33 33; 29 29; 82 82;]
produces
C =[6 6; 3 3; 0 0; 21 21; 17 17; 5 5; 4 4; 33 33; 29 29; 82 82;]
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
I have written a code and i'm pretty much stuck.
In the following code, i have split the complete image into a 3*3 block.
Each of the child (1-8) you can see, say i alter them in that child(1-8) array only.
Is there a method to combine these array (mother & child 1-8) to get the complete image back with the alteration that i've already made
pd_x=imread(name_doc);
[pd_m,pd_n]=size(pd_x);
di_m=pd_m;
di_n=pd_n;
pd_rv=ceil(pd_m/3);
pd_cv=ceil(pd_n/3);
mother1=pd_x(1:pd_rv,1:pd_cv);
child1=pd_x(1:pd_rv,pd_cv:(pd_cv+pd_cv));
child2=pd_x(1:pd_rv,(pd_cv+pd_cv):pd_n);
child3=pd_x(pd_rv:(pd_rv+pd_rv),1:pd_cv);
child4=pd_x(pd_rv:(pd_rv+pd_rv),pd_cv:(pd_cv+pd_cv));
child5=pd_x(pd_rv:(pd_rv+pd_rv),(pd_cv+pd_cv):pd_n);
child6=pd_x((pd_rv+pd_rv):pd_m,1:pd_cv);
child7=pd_x((pd_rv+pd_rv):pd_m,pd_cv:(pd_cv+pd_cv));
child8=pd_x((pd_rv+pd_rv):pd_m,(pd_cv+pd_cv):pd_n);
The syntax for concatenation is the following:
A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6]
A =
12 62 93 -8 22
16 2 87 43 91
-4 17 -72 95 6
Taken from http://www.mathworks.com/help/techdoc/math/f1-84864.html
I've also made a basic example, defining first v,v2 and v3:
>> v
v =
1 2
>> v2
v2 =
3 4
>> v3
v3 =
5 6
I do the following concatenation, the result will be...
>> m = [v v2 v3; v3 v2 v];
>> m
m =
1 2 3 4 5 6
5 6 3 4 1 2
Hope it helps you understand how it works!!
If you used the method I've posted on this answer and still has the 4d matrix created, just do this:
mother1 = permute( mat4d, [ 1 3 2 4 ] );
mother1 = reshape( mother1, [ pd_rv pd_cv ] );
But pd_rv and pd_cv should be calculated with floor, and not with ceil, shouldn't they?