开发者

Help with Compact array-operations in Matlab to replace for-loops

I need to simulate a forest fire and for this, I need a colormap. The colormap has 51 rows, which looks like this:

First 10 is a gradient from dark green to yellow and the rows are generated by:

uint8color = [4 112 31];
for i = -1:8
    cmap = double(uint8(uint8color + i*[30 27 3])) / 255
end;

Next is a single blue line:

cmap(11, :) = [0 0.5 0.9];

And the remaining 40 lines go from yellow over red to almost black, done by the following:

for i=19:-1:-20
    farve = double(uint8(uint8farve + i*[12 12 5])) / 255;
end;

This works fine, however, I would like to avoid using loops wherever possible and try to use the compact array-operations Matlab is capa开发者_Python百科ble of

I'm quite stuck with the above though, not realising how (and if) I can use the compact notation, but still have the index at hand.

For the first 10 rows, something like:

cmap(1:10, :)

Would substitute the for-loop, but it would require me to somehow still extract an index and subtract 2 from the value (1:10 -> -1:8)

I hope there's a Matlab guru somewhere, who can point me in the right direction

Thanks in advance


The first loop can be rewritten as:

uint8color = [4 112 31];
i = (-1:8)';
cmap = double(uint8(repmat(uint8color, length(i), 1) + i*[30 27 3])) / 255;

I'm not quite sure what you're trying to achieve with all those casts, though...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜