Borrar filtros
Borrar filtros

Loops vs. Vectorization

1 visualización (últimos 30 días)
Michael Pietruschka
Michael Pietruschka el 13 de Oct. de 2020
Comentada: Michael Pietruschka el 13 de Oct. de 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 comentarios
Mathieu NOE
Mathieu NOE el 13 de Oct. de 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 el 13 de Oct. de 2020
I edited my question. Thanks for answering

Iniciar sesión para comentar.

Respuesta aceptada

Jon
Jon el 13 de Oct. de 2020
Editada: Jon el 13 de Oct. de 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 comentario
Michael Pietruschka
Michael Pietruschka el 13 de Oct. de 2020
Thank you for your input! I will try it out tomorrow.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by