I don't think I know tricks to use repmat in Matlab yet. I tried a number of combination and I am not able to achieve what I need.
I have a vector A of size 1 x 20. I just want to stack A to create 3 x 5 x 20 size matrix. Can you please help ?
A = 1:20;
reshape(repmat(A, [15, 1]), [3,5,20])
Related
This question already has answers here:
Sum over blocks in a 2D matrix - MATLAB
(4 answers)
Closed 6 years ago.
I'm sure this is basic, but I can't come up to an easy way to reduce a matrix of population count by grid cell by adding up all the elements of the "small" cells which would fit in the new ones.
My matrix is 720x360 and would like to change it to 360x180. I thought about using imresize with 0.5 scale, and then assume the value per new grid is 4 times the old one.
I fell uneasy doing so though, as I can't find examples of similar cases. It'll be nice to have "sum" or "average" as an "interpolation" option or something like that.
Thanks in advance for any help
Cheers
Maria
If you have the Image Processing Toolbox:
x = randi(10,6,8); % example matrix
bs = [2 2]; % block size
y = col2im(sum(im2col(x, bs, 'distinct'), 1), [1 1], size(x)./bs, 'distinct');
How this works:
im2col(... 'distinct') arranges each distinct block of the given size into a column. sum(...,1) then sums each column. Finally, im2col(..., 'distinct') arranges the results back into a matrix of reduced size.
I want to convert a 2D matrix like A into a 3D matrix. Every slice should be the same content like this:
A=[1 2 3;4 5 6;7 8 9];
for i=1:10
B(:,:,i)=A
end
I need the same code without a loop, which decrease the speed of the program. In the original code A and i are rather big.
You can also try
B = A(:,:, ones(1,10) );
Running a small benchmark on ideone shows this approach significantly faster than bsxfun or repmat
I think the simplest and fastest way is using bsxfun, making use of the fact that it expands singleton dimensions:
A=[1 2 3;4 5 6;7 8 9];
B = bsxfun(#times,A,ones(3,3,10))
Here A is seen as a 3 x 3 x 1 matrix, and the third dimension is expanded to match the corresponding dimension in B (ie. 10).
Say we have the following two matrices in matlab:
>> x=[1 5;7 8;9 6]
>> y=[6 87]
I'm trying to make them have the same size. I did that by making the size of y to be the same as the size of x. But, when I call y again, I get the result as the original dimension.
I want them to have the same dimensions since for instance if I want to measure the distance between them they have to have the same dimensions.
So, the other cells can have zero values for instance.
How can I make that in matlab?
Thanks.
Try this :
y = padarray(y,size(x)-size(y),'post');
Be sure that size(x) > size(y).
For more information have a look to : padarray
I want to generate 100x1 matrix with 3 numbers -1,1 and 0. I want to be able to control how much of 1's and -1's are assigned. I tried using
Y = rand(10,1)<0.1
but this only gives me 0's an 1's. But I am able to control the number of 1's in the matrix . Is there a similar type of function that I can use for adding and controlling the number of -1 and 1's along with the default 0. Sorry I am new matlab env.
Thanks
Start by initializing your array:
x = [-1*ones(30,1); zeros(25,1);ones(45,1)];
then use matlab's wonderful indexing with randperm:
y= x(randperm(100));
plot (y, 'o')
I have a 2D matrix of dimensions 64 x 727. What I would like to do is separate each of the columns, creating a 3D matrix of dimensions 64 x 1 x 727.
I have looked through several similar questions on here, but my limited matlab ability is preventing me from applying previous answers to my own issue.
Many thanks,
Robbie
Try
reshape(matrix,64,1,727)
if that doesn't produce what you want explain further.
Try this:
x2d = rand(64, 727);
x3d = reshape(x2d, 64, 1, 727);
Use:
permute(matrix,[1 3 2])
switches 2nd and 3rd dimensions