matlab矩阵 0,matlab zeros初始化为0矩阵
zeros為創(chuàng)建一個(gè)值為零的數(shù)組;
如matrix1=zeros(4,5);%4*5的矩陣,矩陣中每個(gè)元素都為0
matrix2=zeros(4,5,3);%4*5*3的數(shù)組,數(shù)組中每個(gè)元素都為0
下面舉一個(gè)將圖像存到數(shù)組的例子
對RGB圖片1.jpg,2.jpg;大小為700*500*3
創(chuàng)建4D空數(shù)組700*500*3*2
img=zeros(700,500,3,2);
直接將圖片賦予數(shù)組會發(fā)現(xiàn)不是原圖,代碼及運(yùn)行結(jié)果如下:
img1=imread(strcat('C:\Users\Administrator\Desktop\','1.jpg'));
img2=imread(strcat('C:\Users\Administrator\Desktop\','2.jpg'));
% img1_1=im2double(img1);
% img2_2=im2double(img2);
img=zeros(700,500,3,4);
img(:,:,:,1)=img1;
img(:,:,:,2)=img2;
% img(:,:,:,3)=img1_1;
% img(:,:,:,4)=img2_2;
figure(1)
subplot(4,2,1),imshow(img1);
subplot(4,2,2),imshow(img2);
% subplot(4,2,3),imshow(img1_1);
% subplot(4,2,4),imshow(img2_2);
subplot(4,2,5),imshow(img(:,:,:,1));
subplot(4,2,6),imshow(img(:,:,:,2));
% subplot(4,2,7),imshow(img(:,:,:,3));
% subplot(4,2,8),imshow(img(:,:,:,4));
運(yùn)行結(jié)果:
如果將圖片轉(zhuǎn)為雙精度型,則可正常,代碼結(jié)果如下:
img1=imread(strcat('C:\Users\Administrator\Desktop\','1.jpg'));
img2=imread(strcat('C:\Users\Administrator\Desktop\','2.jpg'));
img1_1=im2double(img1);
img2_2=im2double(img2);
img=zeros(700,500,3,4);
img(:,:,:,1)=img1;
img(:,:,:,2)=img2;
img(:,:,:,3)=img1_1;
img(:,:,:,4)=img2_2;
figure(1)
subplot(4,2,1),imshow(img1);
subplot(4,2,2),imshow(img2);
subplot(4,2,3),imshow(img1_1);
subplot(4,2,4),imshow(img2_2);
subplot(4,2,5),imshow(img(:,:,:,1));
subplot(4,2,6),imshow(img(:,:,:,2));
subplot(4,2,7),imshow(img(:,:,:,3));
subplot(4,2,8),imshow(img(:,:,:,4));
結(jié)果
總結(jié)
以上是生活随笔為你收集整理的matlab矩阵 0,matlab zeros初始化为0矩阵的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浮躁的社会
- 下一篇: 收藏!盘点B站up主们最喜欢用的录屏软件