Cant find the number of row for big size of matrix

2 Ansichten (letzte 30 Tage)
fyza affandi
fyza affandi am 4 Jan. 2019
Kommentiert: madhan ravi am 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?

Akzeptierte Antwort

per isakson
per isakson am 4 Jan. 2019
Bearbeitet: per isakson am 4 Jan. 2019
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 Kommentare
per isakson
per isakson am 4 Jan. 2019
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 am 4 Jan. 2019
+1 debugging skills are real good!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

madhan ravi
madhan ravi am 4 Jan. 2019
No loops needed:
[~,e3]=ismember(E3,tblBarray)

Kategorien

Mehr zu Matrix Indexing 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