A shortcut for recurring if statements
1 次查看(过去 30 天)
显示 更早的评论
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
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
2019-7-8
编辑:tiwwexx
2019-7-12
3 个评论
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
2019-7-8
for K = size(tablerow,2)+1 : 5
tablerow(K,:) = tablerow(K-1,:) + [0 50 0 0];
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!