这是什么:uint8((double(weather1618) ./ maxv) .* 50);?
what is this : uint8((double(weather1618) ./ maxv) .* 50);?
这段代码(几乎)完成了我想要的,但我不明白它怎么会这么简单。那么有人可以向我解释这段代码是如何工作的吗?
仅供参考,weather1618 是一个 384x384 数组,范围从 -76 到 -30。而 maxv 的值为 -30.
mapped_array = uint8((double(weather1618) ./ maxv) .* 50);
image(mapped_array);
为什么 .*50 .*100 给出不同的图像而 .*100 .*500 .*1000 是相同的?
如果我直接做,
image(weather1618);
我只会得到蓝色图像。
This code (almost) does what I want but I don't understand how it can
be that simple. So can someone, please, explain me how this code
works?
请注意,此类问题一般为not the best fit for Stack Overflow。但是,既然你已经缩小了你不明白的代码行,我会解释给你看。
你提到过:
FIY, weather1618
is a 384x384 array with a range from -76 to -30.
And maxv
has a value of -30.
第一行代码:
mapped_array = uint8((double(weather1618) ./ maxv) .* 50);
调用以下 functions/operators:
发生了什么:
double(weather1618)
在步骤 2 中将 weather1618
矩阵转换为 double-precision floating-point format, so the values of the matrix are now decimal numbers. In MATLAB, a double can represent numbers ranging from -1.79769e+308 to -2.22507e-308 for negative values and from 2.22507e-308 to 1.79769e+308 for positive values (source). The probable reason for doing this conversion is to avoid an integer division(接下来解释)。
./ maxv
将矩阵的每个元素除以 -30。这将翻转矩阵中每个元素的符号,并将数据缩放 1/30 倍。由于上一步将矩阵转为double,所以除法后得到的数组也是double类型,并且包含小数。
.* 50
将矩阵的每个元素乘以 50。这会将数据缩放 50 倍。乘法后得到的数组将继续为 double 类型,和以前一样。
uint8(...)
将矩阵从 double 类型转换为 uint8 类型 (unsigned integer),因此矩阵的值现在介于 0 到 255 之间。
第二行代码:
image(mapped_array);
调用image
函数显示第4步得到的数组的图像
If I was to do directly,
image(weather1618);
I would only get a blue image.
好发现!之所以只看到蓝色图像,是因为 image
函数默认不使用全部颜色,而 colormap
, so even though the information is there in the image, it is not possible to distinguish it, because it is not being displayed using the full range of colors. On the other hand, the imagesc
函数默认使用全部颜色。
看看我做的这个例子:
img = rand(50); % Random image with values from 0 to 1.
subplot(1, 2, 1); % Left plot.
image(img); % Display image from array.
colorbar; % Colorbar showing color scale.
subplot(1, 2, 2); % Right plot.
imagesc(img); % Display image with scaled colors.
colorbar; % Colorbar showing color scale.
它们是相同的图像,但颜色比例不同(查看颜色条)。
And why does .*50
and .*100
give different images but .*100
,
.*500
and .*1000
are identical?
因为uint8
可以存储的最大值是255,所以任何大于255的值都会被截断为255。这就是为什么乘以100、500和1000没有任何区别,因为值获得全部超过255.
这段代码(几乎)完成了我想要的,但我不明白它怎么会这么简单。那么有人可以向我解释这段代码是如何工作的吗?
仅供参考,weather1618 是一个 384x384 数组,范围从 -76 到 -30。而 maxv 的值为 -30.
mapped_array = uint8((double(weather1618) ./ maxv) .* 50);
image(mapped_array);
为什么 .*50 .*100 给出不同的图像而 .*100 .*500 .*1000 是相同的?
如果我直接做,
image(weather1618);
我只会得到蓝色图像。
This code (almost) does what I want but I don't understand how it can be that simple. So can someone, please, explain me how this code works?
请注意,此类问题一般为not the best fit for Stack Overflow。但是,既然你已经缩小了你不明白的代码行,我会解释给你看。
你提到过:
FIY,
weather1618
is a 384x384 array with a range from -76 to -30. Andmaxv
has a value of -30.
第一行代码:
mapped_array = uint8((double(weather1618) ./ maxv) .* 50);
调用以下 functions/operators:
发生了什么:
double(weather1618)
在步骤 2 中将weather1618
矩阵转换为 double-precision floating-point format, so the values of the matrix are now decimal numbers. In MATLAB, a double can represent numbers ranging from -1.79769e+308 to -2.22507e-308 for negative values and from 2.22507e-308 to 1.79769e+308 for positive values (source). The probable reason for doing this conversion is to avoid an integer division(接下来解释)。./ maxv
将矩阵的每个元素除以 -30。这将翻转矩阵中每个元素的符号,并将数据缩放 1/30 倍。由于上一步将矩阵转为double,所以除法后得到的数组也是double类型,并且包含小数。.* 50
将矩阵的每个元素乘以 50。这会将数据缩放 50 倍。乘法后得到的数组将继续为 double 类型,和以前一样。uint8(...)
将矩阵从 double 类型转换为 uint8 类型 (unsigned integer),因此矩阵的值现在介于 0 到 255 之间。
第二行代码:
image(mapped_array);
调用image
函数显示第4步得到的数组的图像
If I was to do directly,
image(weather1618);
I would only get a blue image.
好发现!之所以只看到蓝色图像,是因为 image
函数默认不使用全部颜色,而 colormap
, so even though the information is there in the image, it is not possible to distinguish it, because it is not being displayed using the full range of colors. On the other hand, the imagesc
函数默认使用全部颜色。
看看我做的这个例子:
img = rand(50); % Random image with values from 0 to 1.
subplot(1, 2, 1); % Left plot.
image(img); % Display image from array.
colorbar; % Colorbar showing color scale.
subplot(1, 2, 2); % Right plot.
imagesc(img); % Display image with scaled colors.
colorbar; % Colorbar showing color scale.
它们是相同的图像,但颜色比例不同(查看颜色条)。
And why does
.*50
and.*100
give different images but.*100
,.*500
and.*1000
are identical?
因为uint8
可以存储的最大值是255,所以任何大于255的值都会被截断为255。这就是为什么乘以100、500和1000没有任何区别,因为值获得全部超过255.