Can anyone simplify this code please?

조회 수: 1 (최근 30일)
jlt199
jlt199 2016년 9월 13일
댓글: jlt199 2016년 9월 15일
Morning all,
I have a problem. I'm trying to take a subset of a matrix, but not a uniform distribution. The difference between points is given in the vector phi, which I've made super long to make sure I don't reach the end of it, As you can see this vector is a 6 number repeating pattern. I've made a vector, zeta, containing the rows I would like to extract from the matrix. But, it is super clunky! Is anyone able to simplify this code for me please? I'm sure it should be possible without the loop.
phi = repmat([8.70;8.70;8.70;8.70;8.70;16.59],10,1);
% Random number generator to decide which sensor to use first
sensor = randi(6,1);
col = 1;
zeta = 1;
while col + round(phi(sensor),0) < 153
col = col + round(phi(sensor),0);
zeta = [zeta; col];
sensor = sensor + 1;
end
Many thanks
  댓글 수: 5
jlt199
jlt199 2016년 9월 13일
Also, my boss reads Matlab so would be good to have sophisticated looking code ;)
jlt199
jlt199 2016년 9월 14일
Does no comments mean that the code is not easily simplifies and I'm ok using a loop in this context? Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

TallBrian
TallBrian 2016년 9월 14일
I am not sure exactly what you mean by simplify but here is some code which eliminates the looping.
LENGTH = 153;
phi = round([8.70; 8.70; 8.70; 8.70; 8.70; 16.59]);
max_reps = round(LENGTH / sum(phi)) + 1;
phi_complete = repmat(phi, max_reps, 1);
phi_offset = [1; phi_complete(randi(6,1):end)];
zeta = cumsum(phi_offset);
zeta = zeta(zeta < LENGTH);
  댓글 수: 1
jlt199
jlt199 2016년 9월 15일
Thanks Brian, this helps a lot

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by