How to find locations of an element in a matrix [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a matrix A, that some elements of this matrix is repeated. I need to find locations of an
element in this matrix. How can I do this?
Thanks.

The function find can give you the row-column indices of elements.
For example
>> [r c] = find( A == 3 )

I hope it will help. Syntax
ind = find(X)
ind = find(X, k)
ind = find(X, k, 'first')
ind = find(X, k, 'last')
[row,col] = find(X, ...)
[row,col,v] = find(X, ...)
Link:
http://www.mathworks.in/help/matlab/ref/find.html

I see you have already got some answers on how to find elements.
Here is how to deal with duplicate elements.
First of all you can find the unique elements:
v = [1:4 2:5] % Suppose this is your vector
[v_unique, idx] = unique(v,'first')
v_unique is now your vector with duplicates removed. However, if you are interested in the locations of the duplicates, this will give you a list:
setxor(idx,1:numel(v))

Related

how to construct a 6 by 6 symmetric matrix of rank 3 in matlab? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I want to construct a 6X6 symmetric matrix A of rank 3 in MATLAB. any suggestions?
tried different ways, couldn't end up with expected result
You can use diag:
diag([1,1,1,0,0,0])
This will produce a diagonal matrix, where the first three entries are ones, and the rest are zeros. Since it is diagonal it is symmetric. And because only three of the diagonal entries are nonzero, it has rank three.
You can try constructing a 6-by-3 matrix H first and using A = H*H' in turn to achieve your objective
>> H = randn(6,3)
H =
1.4090 0.4889 0.8884
1.4172 1.0347 -1.1471
0.6715 0.7269 -1.0689
-1.2075 -0.3034 -0.8095
0.7172 0.2939 -2.9443
1.6302 -0.7873 1.4384
>> A = H*H'
A =
3.0136 1.4837 0.3520 -2.5689 -1.4614 3.1900
1.4837 4.3948 2.9298 -1.0967 4.6978 -0.1542
0.3520 2.9298 2.1218 -0.1661 3.8423 -1.0150
-2.5689 -1.0967 -0.1661 2.2054 1.4282 -2.8940
-1.4614 4.6978 3.8423 1.4282 9.2696 -3.2971
3.1900 -0.1542 -1.0150 -2.8940 -3.2971 5.3464
To verify the resulting A
>> rank(A)
ans =
3
>> ishermitian(A)
ans =
logical
1

Check the a matrix column values, if 0 occurs more than 10 time, than delete the column in matlab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a large matrix, if column values contain more than 10 zeros ('0') , delete the whole column in matlab.
Here is one way you can do it using the sum function and logical operations (see Find Array Elements That Meet a Condition).
% Create matrix.
% First, Second and Fourth columns have 20 ones each.
M = ones(20, 4);
% Third column has 20 zeros.
M(:, 3) = 0;
% Delete columns that have more than 10 zeros.
M(:, sum(M == 0, 1) > 10) = [];
According to this MathWorks discussion, the accepted way of finding the count of zero elements by column is something like
sum(A == 0, 1);
You can use logical indexing to delete all the columns where this is greater than 10:
A = A(:, sum(A == 0, 1) >= 10);

how can I make the index of a matrix increment in this function in matlab [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a matlab function to detect the index array that has data below threshold
the result index_list will be in n x 2 matrix where
index_list(i,1) = row
index_list(i,2) = column
that have element array < threshold
Since I don't know how many result would be in index_list
function [index_list] = pickindex(Array,Threshold)
index = 1;
for i= 1:size(Array,1)
for j = 1:size(Array,2)
if(Array(i,j) <= Threshold)
index_list(index,1)=i;
index_list(index,2)=j;
index = index +1;
end
end
end
This code is working but is there any other suggestion to improve the code?
That sounds like the same thing as:
[r, c] = find(Array <= Threshold);
index_list = [r, c];

Interpolating arrays with different vectors [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have multiple arrays that I need to identify and interpolate to a set number. the set number will be the 'length?' of the biggest array. I need to How could I identify each array length and create a loop to interpolate each array to that specific length? Sorry if I am not providing enough detail.
A = rand(10,2);
B = rand(20,2);
C = rand(5,2);
%find max length, for you cell array you want: max(cellfun(#(x) length(x), MyCellArray))
n = max([length(A), length(B), length(C));
%repeat for each, i.e. loop through the cell array
x = A(:,1);
y = A(:,2);
m = min(x);
M = max(x);
d = (M - m) / n;
xi = m:d:M;
Ai = interp1(x, y, xi);

how get the index of the minimum element of each row of matrix in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am attempting to write a code to find index of the minimum element of each row from the 'distance' matrix, excluding zeroes from the matrix.
If the distance matrix is a (large) sparse matrix, this problem is somewhat more nontrivial. The best approach is probably to subtract the (max value * 2) from each nonzero element in each row. This seems ugly and hackish but I can't think of any other efficient way to solve it.
sub = max(0, max(myArray,[],2) * 2);
[i,j,v] = find(myArray);
v -= sub;
myArray = sparse(i,j,v);
[junk mi] = min(myArray,[],2);
EDIT: There are still precision issues if elements within a row have very different magnitudes. If this is the case, you can take negative inverses instead. In this way, you're not combining magnitudes from different elements in the matrix (or with any constant)
posOnly = ~any(myArray < 0, 2);
[i,j,v] = find(myArray);
inds = posOnly(i);
v(inds) = -1 ./ v(inds);
myArray = sparse(i,j,v);
[junk mi] = min(myArray,[],2);
(Note the use of ~any(myArray < 0) rather than all(myArray >= 0) because (myArray < 0) is at least as sparse as myArray whereas (myArray >= 0) is not sparse)
Using Dan's hint:
myArray(myArray==0)=Inf;
[m mi] = min(myArray, [], 2);
The values of mi will be the index of the minimum element in each row. Note the minimum is taken along the second dimension (per dspyz's suggestion).