A shortcut for recurring if statements

조회 수: 2 (최근 30일)
tiwwexx
tiwwexx 2019년 7월 8일
편집: tiwwexx 2019년 7월 12일
Hello, I have a case where I don't know how many elements a vector will have and then depending on different size vectors I'll either include something or not include something.
For example, what I have now is just
if numtable2rows >=2
tablerow(2,:) = tablerow(1,:) + [0 50 0 0];
end
if numtable2rows >=3
tablerow(3,:) = tablerow(2,:) + [0 50 0 0];
end
if numtable2rows >=4
tablerow(4,:) = tablerow(3,:) + [0 50 0 0];
end
if numtable2rows >=5
tablerow(5,:) = tablerow(4,:) + [0 50 0 0];
end
and I'm just expecting no more than 5 elements in numtable2row.
Does anyone know a more efficient way to do this?
Thank you in advance!
  댓글 수: 2
Stephen23
Stephen23 2019년 7월 9일
@tiwwexx: that algorithm seems unusual, because it discards the existing row data. Is that correct? In any case, please provide some example data, with both input and output matrices.
tiwwexx
tiwwexx 2019년 7월 9일
More like builds off the original row data. Some example data would be "in one case I have 3 rows so numtablerows = 3". I also have a tablerow(1,:) and know that the height of each row is 50 pixels. Hence, if in this table I'm using ocr on each row individually I'll just build all the other row areas from the one I already have.

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

채택된 답변

tiwwexx
tiwwexx 2019년 7월 8일
편집: tiwwexx 2019년 7월 12일
tablerow(1,:) = [a b c d];
for k= 1:numtable2rows
tablerow(k+1,:) = tablerow(k,:) + [0 50 0 0];
end
This works. Inspired by Walter Roberson.
  댓글 수: 3
tiwwexx
tiwwexx 2019년 7월 9일
There we go. I was sloppy in writing this down. numtbale2rows is a number, say '3' one time and '5' another time. then I already have a tablerow(1,:) so I can build off that.
Walter Roberson
Walter Roberson 2019년 7월 9일
You fixed the size() but not the fact that tablerow(k) is a scalar.
tablerow(1,:) = [a b c d];
for k= 1:numtable2rows
tablerow(k+1,:) = tablerow(k, :) + [0 50 0 0];
end

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 7월 8일
for K = size(tablerow,2)+1 : 5
tablerow(K,:) = tablerow(K-1,:) + [0 50 0 0];
end

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by