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)
Related
Suppose I have a matrix a = [1,2,3;4,1,2;3,4,2].
I need to create a logical matrix which is 1 wherever there is an element of b in a. The equivalent of a==4 | a==1 if my array b is a small one like [1,4].
I know one way to do this is:
b = [1,4];
c = logical(zeros(size(a)));
for i=b
c = c | a==i;
end
This solution may not scale well if a and b are large. Is there a cleaner way to do it for larger arrays?
I was hoping a == b would give me what i wanted, but it doesn't.
You can use ismember to output a logical array which is true whenever an element of the first input is a member of the second input. The output is the same size as the first input.
c = ismember( a, b );
In your example:
a = [1,2,3;
4,1,2;
3,4,2];
b = [1,4];
c = ismember( a, b );
% >> c =
% [1,0,0;
% 1,1,0;
% 0,1,0]
I have two 3D matrix A and B. The size of A and B are both 40*40*20 double.
The values in matrix A and B are either 0 or 1. The number of "1" in A are 100,
the number of "1" in B are 50. The "1" in matrix A and B may or may not be in
the same coordinates. I want to get the union of matrix A and B, called C. The values in 3D matrix C is either "1" or "0". The number of "1" in C is less than or equal to 150. My question is how to get the 3D matrix C in Matlab?
You can use the operator or, which is a logical or. So or(a,b) is equivalent to the logical operation a | b.
C = or(A,B);
C = a | b;
| and or are the same operator in MatLab, it's just two different way to call it.
I think this is the best solution as long as it's integrated into MatLab. However, you have plenty different ways to do it.
Just as an example, you can do
C = logical(a+b);
logical is an operator that convert every value into logical values. Long story short, it will replace any value different of 0 by 1.
You can approach it in 2 ways. The more efficient one is using vectors but you can also do it in classical nested for loops.
A = rand(40,40,20);
A = A > 0.01; # Get approximate 320 ones and rest zeros
B = rand(40,40,20);
B = B > 0.005; # Get approximate 160 ones and rest zeros
C = zeros(size(A));
for iter1 = 1:size(A,1)
for iter2 = 1:size(A,2)
for iter3 = 1:size(A,3)
C(iter1,iter2,iter3) = A(iter1,iter2,iter3)|B(iter1,iter2,iter3)
end
end
end
This method will be very slow. You can vectorized it to improve performance
C = A|B
>> 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...
If I have 2 vectors: A with n elements and B with m elements and m < n,
how do I identify all the elements in A which are in B, without using a for loop?
Many thanks
C = intersect(A,B) will give you all the elements which are in both.
There's also ismember(A,B), which will return a logical array indicating for each member of A whether it is also a member of B.
Here is one solution to find which elements of the longer vector (x) are in the shorter vector (y)
x = 1:10;
y = 2:4;
xrep = repmat(x,length(y),1)
yrep = repmat(y',1,length(x))
idx = any(xrep==yrep)