Effacer les filtres
Effacer les filtres

Loops vs. Vectorization

1 vue (au cours des 30 derniers jours)
Michael Pietruschka
Michael Pietruschka le 13 Oct 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 commentaires
Mathieu NOE
Mathieu NOE le 13 Oct 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 le 13 Oct 2020
I edited my question. Thanks for answering

Connectez-vous pour commenter.

Réponse acceptée

Jon
Jon le 13 Oct 2020
Modifié(e) : Jon le 13 Oct 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 commentaire
Michael Pietruschka
Michael Pietruschka le 13 Oct 2020
Thank you for your input! I will try it out tomorrow.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by