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];
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.
>> 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
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 matrix A size 100x100 and matrix B with size 200x200. I want to check whether each element in A is found in B or not and return a vector of common elements. So for example if the first element in A (1,1) is '10' then will check if B has an element '10' or not, if yes then will be added to the resulted common elements vector. So if anyone could please advise.
Use:
[C, ia, ib] = intersect(A,B);
C is the common elements vector, ia contains the indices of A and ib contains the indices of B, such that C = A(ia) and C = B(ib). If you don't want indices, simply use:
C = intersect(A,B);
To search for every elements in A matrix in B matrix You can convert them to row vectors as follows:
A1 = reshape(A, 1, length(A));
B1 = reshape(B, 1, length(B));
And then use intersect.
I'm trying to write a Matlab code that, given a matrix, outputs 3 matrices (according to some rules). I'm having difficulty getting this to work though - I can't output a vector with matrices as entries. I get the error message:
??? In an assignment A(I) = B, the number of elements in B and I
must be the same.
How can I go about doing this?
You could write
function [A B C] = myFunction(X)
A = X;
B = 2 * X;
C = 3 * X;
end
and call it with
[a b c] = myFunction(ones(2))
If you won't want all of the outputs, just call it with
a = myFunction(ones(2))
or
[a b] = myFunction(ones(2))
to get just the first argument, or just the first two arguments.
You can also use cells:
A=cell(1,3); %% or A=cell(1,2); if you want to output only 2 matrices
A{1}=B;
A{2}=C;
A{3}=D;
If your matrices all have the same size you can also concatenate them:
A=zeros(m,n,3);
A(:,:,1)=B;
A(:,:,2)=C;
A(:,:,3)=D;
Function declaration:
function [A, B, C] = something (Input_mat)
%Do whatever needs to be done here, for example:
A= Input_mat;
B= Input_mat';
C= ones(18);
And then when you call it using:
[A,B,C] = something (Some_mat)
A, B and C are filled.