MATLAB Answers

0

Cant find the number of row for big size of matrix

Asked by fyza affandi on 4 Jan 2019
Latest activity Commented on by madhan ravi
on 4 Jan 2019
I have a tblBarray matrix of size 13x1
1
3
2
5
4
10
8
11
12
14
15
16
21
and from given list of E3,
Columns 1 through 18
1 1 4 5 1 1 3 1 1 1 2 5 1 1 1 1 3 4
Columns 19 through 36
1 3 5 10 5 4 4 2 3 4 5 8 4 1 1 2 12 16
Columns 37 through 54
1 1 3 3 1 2 10 14 1 1 1 1 1 1 10 8 2 5
Columns 55 through 64
3 15 5 2 1 3 2 21 1 11
I need to search which row thet are belong to that tblBarray
I use this code below
for nE3=1:length (E3)
e31= find(E3(nE3)==tblBarray);
e3=[e3,e31];
end
And get the answer
1 1 5 4 1 1 2 1 1 1 3 4 1 1 1 1 2
Columns 18 through 34
5 1 2 4 6 4 5 5 3 2 5 4 7 5 1 1 3
Columns 35 through 51
9 12 1 1 2 2 1 3 6 10 1 1 1 1 1 1 6
Columns 52 through 64
7 3 4 2 11 4 3 1 2
Then I try for a bigger size of tblBarray unfortunately, it takes a very long time. Can anyone help me how can I solve this? Do i need to change 'find' function? If so, how can I change it?

  0 Comments

Sign in to comment.

2 Answers

Answer by per isakson
on 4 Jan 2019
Edited by per isakson
on 4 Jan 2019
 Accepted Answer

The editor warns you that there is an issue with your code.
  • Notice the orange signs in the column to the right of the editor text pane.
  • The variable e3 is underlined
Right click the underlined e3
Capture.PNG
If you have not already preallocated e3, you shall do that.
Compare the speed of your loop with this one
%%
e3 = nan( size(E3) );
for nE3=1:length (E3)
e3(nE3) = find(E3(nE3)==tblBarray);
end

  4 Comments

Show 1 older comment
Matlab for-loops have a reputation for being slow. However, nowadays they are much faster than they used to be. In your case, I guess the problem is find, especially if tblBarray is large.
Compare the speed with the solution of madhan ravi . I guess that one is faster.
+1 debugging skills are real good!

Sign in to comment.


Answer by madhan ravi
on 4 Jan 2019

No loops needed:
[~,e3]=ismember(E3,tblBarray)

  0 Comments

Sign in to comment.