>> a=imread ('Vasculature.tif');
>> b = imresize (a, [400,400]);
>> c=imread ('activation.tif');
>> d= imresize (c, [400,400]);
>> e=imadd (b,d);
the code I was able to work with was for horizontal concatenation pls do tell me concatenating the image vertically..
Get the images and make a matrix of them,
use for matrix a and matrix b,
c = vertcat(a,b)
Alternatively, you can use cat:
c = cat(1, a, b);
You can also use straight MATLAB matrix building:
c = [a; b];
This is assuming that the images have the same number of columns. Doing c = [a b] concatenates matrices column-wise. If you want to do it by row, use the semi-colon. Take a look at this basic tutorial on MATLAB operations here to get you started: http://www.mathworks.com/help/matlab/examples/basic-matrix-operations.html
Related
I have two very big column vectors, A and B, of size ax1 and bx1, respectively. I want to construct a vector C of size (b*a)x1 by computing A(i)*B(j) for each i and j. To illustrate what I mean:
clear
a=10^3;
b=10^3;
A=randn(a,1);
B=randn(b,1);
Ctemp=zeros(a,b);
for i=1:a
for j=1:b
Ctemp(i,j)=A(i)*B(j);
end
end
C=reshape(Ctemp, a*b,1);
Question: is there a more efficient way to obtain C which avoid double looping? My actual a and b are bigger than 10^3.
This is a simple case of array multiplication that can benefit from implicit (or explicit) expansion:
% Implicit (R2016b and newer):
C = A(:) .* B(:).'; % optionally surround by reshape( ... , [], 1);
% "Explicit" (R2007a and newer):
C = bsxfun( #times, A(:), B(:).' );
From there it's just a matter of reshaping, as you're already doing (D = C(:) or D = C(:).').
You can also calculate the outer product of the vectors, resulting in a matrix of your required terms:
C = A*B'; % Assuming A,B are column vectors here
And reshape the output afterward as stated. Not sure if more efficient though.
I have a 2000x1 vector called A and want to add another 2000x1, vector B, to the first one. The idea is to create a 2000x2 matrix.
I'm using the following code:
A= [A; B];
The result I'm getting is a 4000x1 vector though. Can anyone help me.
Thanks.
In MATLAB the first dimension of a matrix is interpreted as the number of rows and the [_;_] is the vertical stacking operator, so if you have
size(A) == [2000 1]
size(B) == [2000 1]
then
size([A;B]) == [4000 1]
and
size([A B]) == [2000 2]
so to get a 2000x2 matrix you need to concatenate the two vectors with the [_ _] operator as [A B]
You want to perform horizontal concatenation of two arrays. There are two ways to solve your problem.
1) As Dmitri Chubarov stated: A = [A B] will give you the desired result.
2) There is a function horzcat which does the same thing. A = horzcat(A, B);
Concatenation in the other direction is achieved by the means as Z = vertcat(A, B); and Z = [A; B];
Suppose I have two matrices A and B that are 3d. A = 49x49x18 B = 49x49x24 After concatenation I want to see the C = 49x49x42 as the concatenation of A and B at the third dimension.
How would I do that at matlab ?
Use the cat function specifying dimension 3
C = cat(3, A, B)
I have a 7x21 matrix called A. Within this matrix there are three equally sized 7x7 submatrices. I call them B,C and D, where B = A(:,1:7), C = A(:,8:14) and D = A(:,15:21).
How can I produce a matrix E which is also 7x7 matrix where simply B, C and D are added up, i.e. E = B+C+D.
Thanks a lot for your help!
Generic code to get such an output -
N = 3; %// Number of submatrices
[m,n] = size(A) %// Get size [no. of cols must be multiple of N
E = reshape(sum(reshape(A,m*n/N,[]),2),m,n/N)
I don't see what's going to be more straightforward and concise than
E = A(:,1:7) + A(:,8:14) + A(:,15:21)
Unless you need an expression that generalizes in some way you're not describing...
I have four 52-by-140 matrices in Matlab. Lets call them a, b, c, and d:
I want to apply the eigs function to a Hessian matrix [a,b;c,d] for every point in the original matrix like:
for i = 1:52
for j = 1:140
eigs([a(i,j),b(i,j);c(i,j),d(i,j)])
end
end
How can I do this in a simpler way, i.e., without for loops?
This can probably be done using arrayfun and defining the action you do in a single iteration using an anonymous function (untested)
result = arrayfun(#(a_ij, b_ij, c_ij, d_ij) eigs([a_ij, b_ij; c_ij, d_ij]), ...
a, b, c, d, 'uniformoutput', false);
Note that since eig returns a vector, the result can only be stored in a cell array.
You could name the parameters of the anonymous functions a, b, ... instead of a_ij, b_ij, ..., since they are only used inside the function, but I prefer to it this way to make it clear that inside the function you are using scalars, while the parameters to arrayfun are matrices. Personally, I often use upper/lower case to indicate the difference:
result = arrayfun(#(a, b, c, d) eigs([a, b; c, d]), A, B, C, D, 'uni', 0);
but then you would have to rename your variables.
Try this solution to get all of the matrixes teed up:
>> abcd = cat(3,a,b,c,d);
>> H = permute(reshape(permute(abcd,[3 1 2]),2,2,[]),[2 1 3]);
>> size(H)
ans =
2 2 7280
>> i=3;j=2;
>> [a(i,j),b(i,j);c(i,j),d(i,j)]
ans =
0.4984 0.7935
0.3524 0.2273
>> H(:,:,i+(j-1)*size(abcd,1))
ans =
0.4984 0.7935
0.3524 0.2273
>>
Then to run eigs on all 2D matrixes in H:
E=arrayfun(#(i)eigs(H(:,:,i)),1:size(H,3),'uni',false);