a=imread('autumn.tif'); c = zeros(size(a)); pos = find((a >= 60) & (a < 105)); c(pos) = a(pos); b(:, :, 3) = c; c = zeros(size(a)); pos = find((a >= 105) & (a < 150)); % 此处的 pos 也需要重新赋值 pos = a(pos); % 加上这一行 c(pos) = a(pos); b(:, :, 2) = c; c = zeros(size(a)); pos = find(a >= 150); c(pos) = a(pos); b(:, :, 1) = c; b = uint8(b); figure; imshow(b);matlab代码运行报错怎么解决
时间: 2024-01-15 14:03:53 浏览: 94
图形加密的算法a=imread('tu1.bmp'); ws1=size(a); %read secret image
根据给出的代码,出现错误的原因可能是在第6行和第8行,`pos` 变量的类型应该是逻辑数组,但是在代码中 `pos` 变量被赋值为数值数组,因此会出现错误。
解决方法:将第6行和第8行的 `pos` 变量重新赋值为逻辑数组,即将原来的 `pos = a(pos)` 改为 `pos = find((a >= 105) & (a < 150))`。修改后的代码如下:
```
a=imread('autumn.tif');
c = zeros(size(a));
pos = find((a >= 60) & (a < 105));
c(pos) = a(pos);
b(:, :, 3) = c;
c = zeros(size(a));
pos = find((a >= 105) & (a < 150));
pos = find((a >= 105) & (a < 150)); % 修改这一行
c(pos) = a(pos);
b(:, :, 2) = c;
c = zeros(size(a));
pos = find(a >= 150);
c(pos) = a(pos);
b(:, :, 1) = c;
b = uint8(b);
figure;
imshow(b);
```
运行修改后的代码应该就不会报错了。
阅读全文