-1

I have

result=
   [1   0   0
    2   0   0
    3   0   0
    1   2   0
    1   3   0
    2   3   0
    1   2   3
    4   0   0
    5   0   0
    6   0   0
    4   5   0
    4   6   0
    5   6   0
    4   5   6
    7   0   0
    8   0   0
    9   0   0
    7   8   0
    7   9   0
    8   9   0
    7   8   9]

I want find for example 1.
I wrote this code:

[o,p]=find(result==1)
 for i=1 : numel(o)
 member(i,:)=result(o(i),:)
 end

after run it shows

member=[1     0     0
        1     2     0
        1     3     0
        1     2     3]

and it's correct.
My question is when my matrix is bigger, for example this matrix

result =
[0.142  0   0   0   0
0.2635  0   0   0   0
0.2589  0   0   0   0
0.2259  0   0   0   0
0.1096  0   0   0   0
0.142   0.2635  0   0   0
0.142   0.2589  0   0   0
0.142   0.2259  0   0   0
0.142   0.1096  0   0   0
0.2635  0.2589  0   0   0
0.2635  0.2259  0   0   0
0.2635  0.1096  0   0   0
0.2589  0.2259  0   0   0
0.2589  0.1096  0   0   0
0.2259  0.1096  0   0   0
0.142   0.2635  0.2589  0   0
0.142   0.2635  0.2259  0   0
0.142   0.2635  0.1096  0   0
0.142   0.2589  0.2259  0   0
0.142   0.2589  0.1096  0   0
0.142   0.2259  0.1096  0   0
0.2635  0.2589  0.2259  0   0
0.2635  0.2589  0.1096  0   0
0.2635  0.2259  0.1096  0   0
0.2589  0.2259  0.1096  0   0
0.142   0.2635  0.2589  0.2259  0
0.142   0.2635  0.2589  0.1096  0
0.142   0.2635  0.2259  0.1096  0
0.142   0.2589  0.2259  0.1096  0
0.2635  0.2589  0.2259  0.1096  0
0.142   0.2635  0.2589  0.2259  0.1096
0.1638  0   0   0   0
0.223   0   0   0   0
0.3345  0   0   0   0
0.0637  0   0   0   0
0.215   0   0   0   0
0.1638  0.223   0   0   0
0.1638  0.3345  0   0   0
0.1638  0.0637  0   0   0
0.1638  0.215   0   0   0
0.223   0.3345  0   0   0
0.223   0.0637  0   0   0
0.223   0.215   0   0   0
0.3345  0.0637  0   0   0
0.3345  0.215   0   0   0
0.0637  0.215   0   0   0
0.1638  0.223   0.3345  0   0
0.1638  0.223   0.0637  0   0
0.1638  0.223   0.215   0   0
0.1638  0.3345  0.0637  0   0
0.1638  0.3345  0.215   0   0
0.1638  0.0637  0.215   0   0
0.223   0.3345  0.0637  0   0
0.223   0.3345  0.215   0   0
0.223   0.0637  0.215   0   0
0.3345  0.0637  0.215   0   0
0.1638  0.223   0.3345  0.0637  0
0.1638  0.223   0.3345  0.215   0
0.1638  0.223   0.0637  0.215   0
0.1638  0.3345  0.0637  0.215   0
0.223   0.3345  0.0637  0.215   0
0.1638  0.223   0.3345  0.0637  0.215
0.1772  0   0   0   0
0.2412  0   0   0   0
0.2585  0   0   0   0
0.2068  0   0   0   0
0.1163  0   0   0   0
0.1772  0.2412  0   0   0
0.1772  0.2585  0   0   0
0.1772  0.2068  0   0   0
0.1772  0.1163  0   0   0
0.2412  0.2585  0   0   0
0.2412  0.2068  0   0   0
0.2412  0.1163  0   0   0
0.2585  0.2068  0   0   0
0.2585  0.1163  0   0   0
0.2068  0.1163  0   0   0
0.1772  0.2412  0.2585  0   0
0.1772  0.2412  0.2068  0   0
0.1772  0.2412  0.1163  0   0
0.1772  0.2585  0.2068  0   0
0.1772  0.2585  0.1163  0   0
0.1772  0.2068  0.1163  0   0
0.2412  0.2585  0.2068  0   0
0.2412  0.2585  0.1163  0   0
0.2412  0.2068  0.1163  0   0
0.2585  0.2068  0.1163  0   0
0.1772  0.2412  0.2585  0.2068  0
0.1772  0.2412  0.2585  0.1163  0
0.1772  0.2412  0.2068  0.1163  0
0.1772  0.2585  0.2068  0.1163  0
0.2412  0.2585  0.2068  0.1163  0
0.1772  0.2412  0.2585  0.2068  0.1163
0.1221  0   0   0   0
0.2792  0   0   0   0
0.2557  0   0   0   0
0.2393  0   0   0   0
0.1037  0   0   0   0
0.1221  0.2792  0   0   0
0.1221  0.2557  0   0   0
0.1221  0.2393  0   0   0
0.1221  0.1037  0   0   0
0.2792  0.2557  0   0   0
0.2792  0.2393  0   0   0
0.2792  0.1037  0   0   0
0.2557  0.2393  0   0   0
0.2557  0.1037  0   0   0
0.2393  0.1037  0   0   0
0.1221  0.2792  0.2557  0   0
0.1221  0.2792  0.2393  0   0
0.1221  0.2792  0.1037  0   0
0.1221  0.2557  0.2393  0   0
0.1221  0.2557  0.1037  0   0
0.1221  0.2393  0.1037  0   0
0.2792  0.2557  0.2393  0   0
0.2792  0.2557  0.1037  0   0
0.2792  0.2393  0.1037  0   0
0.2557  0.2393  0.1037  0   0
0.1221  0.2792  0.2557  0.2393  0
0.1221  0.2792  0.2557  0.1037  0
0.1221  0.2792  0.2393  0.1037  0
0.1221  0.2557  0.2393  0.1037  0
0.2792  0.2557  0.2393  0.1037  0
0.1221  0.2792  0.2557  0.2393  0.1037
0.1638  0   0   0   0
0.223   0   0   0   0
0.3345  0   0   0   0
0.0637  0   0   0   0
0.215   0   0   0   0
0.1638  0.223   0   0   0
0.1638  0.3345  0   0   0
0.1638  0.0637  0   0   0
0.1638  0.215   0   0   0
0.223   0.3345  0   0   0
0.223   0.0637  0   0   0
0.223   0.215   0   0   0
0.3345  0.0637  0   0   0
0.3345  0.215   0   0   0
0.0637  0.215   0   0   0
0.1638  0.223   0.3345  0   0
0.1638  0.223   0.0637  0   0
0.1638  0.223   0.215   0   0
0.1638  0.3345  0.0637  0   0
0.1638  0.3345  0.215   0   0
0.1638  0.0637  0.215   0   0
0.223   0.3345  0.0637  0   0
0.223   0.3345  0.215   0   0
0.223   0.0637  0.215   0   0
0.3345  0.0637  0.215   0   0
0.1638  0.223   0.3345  0.0637  0
0.1638  0.223   0.3345  0.215   0
0.1638  0.223   0.0637  0.215   0
0.1638  0.3345  0.0637  0.215   0
0.223   0.3345  0.0637  0.215   0
0.1638  0.223   0.3345  0.0637  0.215]

So when I search for 0.142 using that code it doesn't give an error but it gives the warning

The variable appears to change size on every loop iteration. Consider preallocating for speed.

and answer returns [], meaning it can't find 0.142. So what is the problem and how do I solve it?

beaker
  • 16,331
  • 3
  • 32
  • 49
  • 5
    Might be helpful - http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab?s=1|3.4299 – Divakar Apr 09 '16 at 11:03
  • 1
    I haven't read your question.. but I don't think that's what a minimal example should look like... – Matthias W. Apr 10 '16 at 12:35
  • The reason your code does not find any values is not because of the size of the matrix, but that the values stored in the matrix are not equal to `0.142`, despite what MATLAB is showing you. Try `format long` and see what the values of `result` look like. – beaker Apr 10 '16 at 16:12
  • yes your right its solved Thanks sir – mojtaba dad Apr 10 '16 at 16:27

1 Answers1

2

So for the warning to disapear you can put the following line above your for loop:

member = zeros(length(o),size(result,2));

This pre-allocates the size before you begin to assign to member matrix. Hence Matlab knows what the size is going to be.

Coming to your code it works perfectly fine. Consider also modifying the find function as follows:

[o,p]=find(result==0.142)
Adilli Adil
  • 1,231
  • 1
  • 17
  • 25