This question already has answers here:
Element-wise array replication in Matlab
(7 answers)
Closed 7 years ago.
i have an array in matlab software like this:
X=[x1,x2,x3];
And, I want to change this array to be like this:
X=[x1,x1,x2,x2,x3,x3];
Is there any command for doing this work in the simplest way ?
Use reshape and repmat like this
a=[1 2 3];reshape(repmat(a, 2, 1), 1, [])
repmat creates the amount of entries and reshape orders it as you asked.
Related
This question already has answers here:
How do I find a specific cell within a cell array?
(2 answers)
Closed 6 years ago.
I have a cell like
A= {[7,7]; [7,4,7]; [7,7]; [7,7]; [4,5]};
I want to count the number of [7,7] elements.
How can I do this in MATLAB without using a loop?
Thanks.
You could use cellfun with isequal
sum(cellfun(#(x) isequal(x, [7 7]), A))
ans =
3
This question already has answers here:
Run-length decoding in MATLAB
(4 answers)
Closed 6 years ago.
I have a variable distr=[0 3 1 0 2];, and I have a variable full which should contrain distr(i) times i, for all i.
In this example, i want:
full=[2 2 2 3 5 5];
because distr(2)=3, therefore 3x 2, and so on.
Of course I can do it in a for-loop:
full=zeros([1,sum(distr)]);
cc=1;
for i=1:length(distr)
curr=distr(i);
full(cc:cc+curr-1)=i*ones([1,curr]);
cc=cc+curr;
end
but that is very slow. Do you know of a fast way, using MATLAB's awesome array-oriented style? Thanks!
Not sure, but maybe this will work. I can't check it since I currently don't have MATLAB:
full_tmp = arrayfun(#(i,n) i*ones(1,n),1:length(distr),distr,'uniformoutput',false);
full = cat(2,full_tmp{:});
This question already has answers here:
How can I change the values of multiple points in a matrix?
(3 answers)
Closed 6 years ago.
I have a 2-D array. And I want to access it with rows and colums index stored in another 2-D array.
Example: Now I don't want to use loops but I want to access A(1, 2) and A(3, 4).
A = ones(10,10)
B = [1, 2 ; 3, 4]
If I do A(b(:,1), b(:,2)), this will result in all possible combination of [1,2] and [3,4].
How can it be done?
Use MATLAB's sub2ind function:
A(sub2ind(size(A),B(:,1),B(:,2)))
This question already has answers here:
how to replicate an array
(4 answers)
Closed 8 years ago.
Could you help me please to repeat matrix. for example if I have matrix(A) and I want to create a big matrix(B) contains three matrices of matrix(A) in the row and two matrices in the column.
The MATLAB function repmat does exactly what you need:
B = repmat(A,3,2);
For more details see the MATLAB documentation
In MatLab, you want to use repmat().
In Python, use the Numpy function, tile(a, (m, n)).
You should check out this post: https://stackoverflow.com/a/1724410/515559
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Understanding why I can't use index on the result of the size function on a matrix in matlab
(1 answer)
Closed 8 years ago.
Doesn't Matlab allow chaining of matrix operations with indexing?
For eg.:
a = [1 2; 3 4];
exp(a)(:)
throws the error
Error: ()-indexing must appear last in an index expression.
It seems like this is something I would have expected Matlab to have or is there a different way to do this?
You don't need the indexing colon. The below should work.
a = [1 2; 3 4];
exp(a);