matlab中for循环的使用

for i=1:1000
if(a(1,i)>0.5)
a(1,i)=1;
else
a(1,i)=-1;
end;
end;
请解释一下这里的if,else语句具体意思
a=rand(1,m);
for i=1:m
if(a(1,i)>0.5)
a(1,i)=1;
else
a(1,i)=-1;
end;
end;
a
程序原来是这样的,m是表示什么意思

不是空的,赋值的时候出错了,我改了一点,不知道对你有用没。。
% clear;close all
I=imread('1.jpg');%读入原始jpg格式图像
figure
plot(1,1)
imshow(I)
W=size(I,2); %得到图像高度
I1=rgb2gray(I);%将原图像转化为灰度图象
figure;subplot(221);
imshow(I1);title('灰度图像');
I2=medfilt2(I1);%滤波默认窗口为[3,3]
subplot(222);imshow(I2);title('中值滤波结果');
I3=filter2(fspecial('average',3),I1)/255; %模板尺寸为3
subplot(223);imshow(I3);title('均值滤波结果');
I4=wiener2(I1,[3 3]); %对图像进行二维自适应维纳滤波
subplot(224);imshow(I4); title('自适应滤波结果');
J=[I1,I2,I3,I4];%这里的矩阵为什么是空的?
for j=1:4;
Ij=J(:,1+W*(j-1):W*j); %%给Ij赋值
BW1=edge(Ij,'prewitt');%边缘检测
BW2=edge(Ij,'canny');
BW3=edge(Ij,'log');
BW4=edge(Ij,'sobel');
figure;
subplot(221);imshow(BW1);title('prewitt算子');
subplot(222);imshow(BW2);title('canny算子');
subplot(223);imshow(BW3);title('laplacian算子');
subplot(224);imshow(BW4);title('sobel算子');
end
figure;subplot(121);imhist(I1);title('灰度直方图');%观察灰度直方图, 灰度200处有谷,确定阈值T=200
I5=im2bw(I1,220/255); % im2bw函数需要将灰度值转换到[0,1]范围内
subplot(122);imshow(I5);title('直方图阈值分割效果');
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-11-26
如果数组a中第一行的1000个元素中有大于0.5的,则将这个元素重新赋值为1,其它情况(等于或小于0.5)则重新赋值为-1。m是让rand函数产生多少列的数组,rand(1,m)表示产生1行m个列的随机数组。追问

下面那段程序中a=rand(1,m)中的m是什么意思

追答

m是让rand函数产生多少列的数组,rand(1,m)表示产生1行m个列的随机数组。

追问

请问如何循环产生随机数组

追答

a=rand(1,m);放到for语句下面,
for i=1:m
a=rand(1,m);
if(a(1,i)>0.5)
。。。。

本回答被提问者采纳
相似回答