Loops vs. Vectorization

1 回表示 (過去 30 日間)
Michael Pietruschka
Michael Pietruschka 2020 年 10 月 13 日
コメント済み: Michael Pietruschka 2020 年 10 月 13 日
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 件のコメント
Mathieu NOE
Mathieu NOE 2020 年 10 月 13 日
hello
what are you looking for ? faster code ?
maybe if you coud provide an input data file to test it...
Michael Pietruschka
Michael Pietruschka 2020 年 10 月 13 日
I edited my question. Thanks for answering

サインインしてコメントする。

採用された回答

Jon
Jon 2020 年 10 月 13 日
編集済み: Jon 2020 年 10 月 13 日
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 件のコメント
Michael Pietruschka
Michael Pietruschka 2020 年 10 月 13 日
Thank you for your input! I will try it out tomorrow.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by