Find and count matching cell elements [duplicate] - matlab

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

Related

How to index and change a value within a matrix using a variable? [duplicate]

This question already has answers here:
Use a vector as an index to a matrix
(3 answers)
Compact MATLAB matrix indexing notation
(3 answers)
Closed 4 years ago.
For example, I have a variable i and I want to set i=(2,3) such that B(i)=B(2,3)=7.
B=[1 2 3 4
5 6 7 8];
i=('2,3');
B(i)
I am kind of stuck here. Please help.

MATLAB: fast creation of vector of indices [duplicate]

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{:});

Is there anyway to avoid loop in matlab array accessing? [duplicate]

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)))

How can i duplicate an array elements [duplicate]

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.

Find the value in a matrix with certain condition [duplicate]

This question already has answers here:
Find vector elements matching two conditions in Matlab
(3 answers)
Closed 9 years ago.
How to find the value in a matrix with certain condition. For example,
a=[-3.14,2.12,-5,3,6,7];
b=find(a>0)
this will return the indices of matrix with that ">0" condition, which is b= 2 4 5 6.
do we have any solution for find the actual value in a matrix under that condition, such as returning b= 2.12 3 6 7 ?
you can even skip the find part:
whatyouwant = a(a>0);
That's called logical indexing in Matlab...
You could do the following
a = [-3.14,2.12,-5,3,6,7];
b = find(a>0)
c = a(b)
The c would then be the selected values based on the indices in b.
Hope it helps!