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 CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by