Cant find the number of row for big size of matrix

조회 수: 3 (최근 30일)
fyza affandi
fyza affandi 2019년 1월 4일
댓글: madhan ravi 2019년 1월 4일
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?

채택된 답변

per isakson
per isakson 2019년 1월 4일
편집: per isakson 2019년 1월 4일
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
per isakson
per isakson 2019년 1월 4일
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.
madhan ravi
madhan ravi 2019년 1월 4일
+1 debugging skills are real good!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 4일
No loops needed:
[~,e3]=ismember(E3,tblBarray)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by