Filter löschen
Filter löschen

Loops vs. Vectorization

1 Ansicht (letzte 30 Tage)
Michael Pietruschka
Michael Pietruschka am 13 Okt. 2020
How can I replace this:
for i = 1:size(zensus,1)
disp(i)
for j = 1:size(spezQ,1)
if zensus.GBT(i)==spezQ.GBT(j)&&zensus.BJR(i)==spezQ.BJR(j)&&zensus.ZLW(i)==spezQ.ZLW(j)
zensus.SQ(i) = spezQ.SQ(j);
end
end
end
with a vectorized version?
I am trying to assign certain values (spezQ) to combinations of categories in my data (zensus).
Any help would be incredible.
Edit: The data looks like this:
zensus: [205240x6]
HZT FLW ZLW HHG GBT BJR SQ
1 1 1 1 1 1 ?
1 1 1 2 2 1 ?
1 1 2 1 1 1 ?
spezQ: [27x4]
GBT BJR ZLW SQ
1 1 1 212,45325
1 1 2 192,6525
1 1 3 183,0135
1 2 2 161,2365
For example: zensus.SQ(1) should be equal to spezQ.SQ(1) because of the matching values of GBT, BJR and ZLW.
My loop takes forever because of the length of zensus. So I am looking for faster code!
  2 Kommentare
Mathieu NOE
Mathieu NOE am 13 Okt. 2020
hello
what are you looking for ? faster code ?
maybe if you coud provide an input data file to test it...
Michael Pietruschka
Michael Pietruschka am 13 Okt. 2020
I edited my question. Thanks for answering

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jon
Jon am 13 Okt. 2020
Bearbeitet: Jon am 13 Okt. 2020
It may be useful for you to know that for example
M = [3 5 7] == [2; 3; 4;] % row vector == column vector
gives a matrix
3×3 logical array
0 0 0
1 0 0
0 0 0
You can find the matching row and column using indices
[i,j] = find (M)
i =
2
j =
1
So you could probably do something like:
[i,j] = find(zensus.GBT==spezQ.GBT'&&zensus.BJR==spezQ.BJR'&&zensus.ZLW==spezQ.ZLW')
zensus.SQ(i) = spezQ.SQ(j)
I may have mixed up the i's and j's but I think you will get the idea
  1 Kommentar
Michael Pietruschka
Michael Pietruschka am 13 Okt. 2020
Thank you for your input! I will try it out tomorrow.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operating on Diagonal 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