How I can know elements of a matrix

1 Ansicht (letzte 30 Tage)
Sebastian G
Sebastian G am 7 Mär. 2013
I need know the elements of a matrix if true
I=round(rand(10,10)*1000);
What are the elements of this matrix
Thank you very much
  1 Kommentar
Jan
Jan am 7 Mär. 2013
A small example of the wanted output would avoid, that we waste your and our time with guessing, what you are searching for. Please add an example with a 2x2 matrix by editing the question (not as comment or answer).

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 7 Mär. 2013
Another guess:
A = round(rand(10,10,3) * 1000);
existingElements = unique(A(:));

Weitere Antworten (5)

Carlos
Carlos am 7 Mär. 2013
When you multiply the matrix by 1000 you have binary values(1 and 0 true false)no more.So I don´t know what you exactly mean by true. However, assuming you have a 0 and 1 valued matrix, you could do the following
>> I=round(rand(10,10));
>> I
I =
0 0 0 0 1 0 1 0 1 1
1 0 1 1 1 0 0 0 0 1
0 0 0 0 0 1 0 1 1 0
1 1 1 0 1 1 0 0 0 0
0 0 1 0 0 0 0 0 1 0
1 1 1 0 0 0 0 0 0 0
0 1 0 1 0 0 1 1 0 1
1 1 0 1 0 1 1 1 1 1
1 0 0 1 0 0 1 1 1 1
1 0 1 0 0 0 0 0 0 1
Now using the find command you can find out the indices that contain a 1.
>> find(I==1)
ans =
2
4
6
8
9
10
14
16
17
18
22
24
25
26
30
32
37
38
39
41
42
44
53
54
58
61
67
68
69
73
77
78
79
81
83
85
88
89
91
92
97
98
99
100
However you have to translate these index values to get what you want, for instance the index 100 corresponds to the matrix position I(10,10), the index 99 to the matrix position I(9,9), the index 88 refers to I(8,8) and so on.
  1 Kommentar
Sebastian G
Sebastian G am 7 Mär. 2013
I have 1000 in the matrix elements as possible and I have to see which ones are.

Melden Sie sich an, um zu kommentieren.


Sebastian G
Sebastian G am 7 Mär. 2013
I have 1000 in the matrix elements as possible and I have to see which ones are.
  2 Kommentare
Jan
Jan am 7 Mär. 2013
I do not understand this sentence.
Carlos
Carlos am 7 Mär. 2013
Neither do I

Melden Sie sich an, um zu kommentieren.


Carlos
Carlos am 7 Mär. 2013
If you want to find if you have a 1000 in your matrix try the same thing, but change 1 for 1000
find(I==1000)
Will return
ans =
100
If the element I(10,10) is 1000
  1 Kommentar
Sebastian G
Sebastian G am 7 Mär. 2013
I have 1000 potential in the matrix element.
I=round(rand(10,10)*1000);

Melden Sie sich an, um zu kommentieren.


Sebastian G
Sebastian G am 7 Mär. 2013
This method is a possible but not optimal. There must be something faster.
I=round(rand(10,10,3)*1000);
n=1;
for i=1:1000;
a=sum(find(I==i));
if a>=1;
elm(n)=i;
n=n+1;
end
end

Image Analyst
Image Analyst am 7 Mär. 2013
Bearbeitet: Image Analyst am 7 Mär. 2013
Not sure what you want. Virtually every one of the elements in (the badly-named) I will be considered as true. Only those few which might happen to have a value of 0 will be considered as false. All others, with values from 1 to 1000 will be considered as true in a logical test, like an if statement.
From your answer above I can't tell if you're trying to build a histogram of values, or something else. If you want a histogram, just call hist():
[counts, values] = hist(I, numberOfBins);

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by