MATLAB Answers

I have a matrix with values from -180 to 180 and i want to find all the places where each value is and then save them in a new matrix, how can i do it?

1 view (last 30 days)
abbxucy11
abbxucy11 on 12 Sep 2016
Commented: mbonus on 13 Sep 2016
I want to create a for loop that goes for values -180:1:180 and finds all the locations(i,j) for each value. e.g i want to find all the locations for value -180 and then save them in a new matrix. is it possible to be done? i wrote here the code for the loop function and it works but i dont know how to save the values so as to know which location is for each value.
for a= 180:-1:-180
[i,j]=find(ORIENT==a)
end

  0 Comments

Sign in to comment.

Answers (2)

mbonus
mbonus on 12 Sep 2016
Is this what you're looking for?
Result = [];
for a = -180:180
[ind1,ind2] = find(ORIENT == a);
Result(a,1) = ind1;%save the row number to row a
Result(a,2) = ind2;%save the column number to row a
try
Result(a,3) = ORIENT(ind1,ind2);%save the value for the indices ind1&ind2
catch
Result(a,3) = NaN;%save NaN if there is no corresponding value for the indices
end
end

  2 Comments

abbxucy11
abbxucy11 on 13 Sep 2016
unfortunately this doesnt work. Basically i want to find a way to save the results in two different matrixes, one for the i's and one for the j's.. any ideas? your code makes senses but it has an error at the line: result (a,1)
mbonus
mbonus on 13 Sep 2016
Are you getting multiple values returned for each index in the find? I assumed that there was only one of each value in the matrix ORIENT. Do you want every i in a single matrix and every j in a single matrix? if so I would suggest a cell array because that allows you to have different column or row lengths if there is an unknown number of each value of a in the matrix ORIENT

Sign in to comment.


Star Strider
Star Strider on 12 Sep 2016
Since ‘a’ is a vector, ‘j’ will always be 1. So forget about the loop and just calculate them directly:
i = 1:361;
a = 181 - i;

  0 Comments

Sign in to comment.