I want to find the index of zero elements in the upper triangle of the matrix inside a cell array. Assume that I have a cell array A{1,1}
and there is a matrix B
of size 19-by-19 inside the first cell of A
. How can I find the index of 0
elements on the upper triangle of the B
matrix?
I tried to use the answer in this link and I wrote this code:
which(upper.tri(Adjecany_Valid_vertices{1,1}) & Adjecany_Valid_vertices{1,1}==0, arr.ind=TRUE)
but it didn't work for me and it gives me an error:
The expression to the left of the equals sign is not a valid target for an assignment.
does anybody have any solution for finding these requested zeros?
A{1,1}
is not a cell array, it is one cell in a cell array 'A'. But your question does not really have to do anything with cell arrays. Your question is how do I find the indices of all zeros in only the upper triangular part of a matrix. The code you are looking for is:
idx = triu(B==0)
idx
will be a two dimensional matrix of logicals with true wherever the element is zero. All the entries in lower triangular part will be false. You can use this matrix like
t = B(idx)
to get all the zero elements as a single column array. If you want to get indices in an i,j
format you would have to do:
[i,j] = ind2sub(size(B), find(triu(B == 0)))
I would suggest looking up triu
, ind2sub
and find
in the documentation