How can I simplify my code using loops?
1 次查看(过去 30 天)
显示 更早的评论
Below is my code for image processing
I'd like to simplify the underlined area of my code using loops, but I don't know how to do it.
Is there anyone to help me?
----------------------------------------------------------------------------
clc;
clear all;
lena=imread('lena.ppm');
lena_ch1=lena(:,:,1);
% The first of unknown channels
lena_ch2=lena(:,:,2);
% The second of unknown channels
lena_ch3=lena(:,:,3);
% The third of unknown channels
black_ch=zeros(size(lena,1),size(lena,2));
% Black channel which has 0 components as all of pixel image levels
lena_1=cat(3,lena_ch1,lena_ch2,lena_ch3);
lena_2=cat(3,lena_ch1,lena_ch3,lena_ch2);
lena_3=cat(3,lena_ch2,lena_ch1,lena_ch3);
lena_4=cat(3,lena_ch2,lena_ch3,lena_ch1);
lena_5=cat(3,lena_ch3,lena_ch1,lena_ch2);
lena_6=cat(3,lena_ch3,lena_ch2,lena_ch3);
subplot(1,factorial(size(lena,3)),1); imshow(lena_1);
subplot(1,factorial(size(lena,3)),2); imshow(lena_2);
subplot(1,factorial(size(lena,3)),3); imshow(lena_3);
subplot(1,factorial(size(lena,3)),4); imshow(lena_4);
subplot(1,factorial(size(lena,3)),5); imshow(lena_5);
subplot(1,factorial(size(lena,3)),6); imshow(lena_6);
0 个评论
回答(1 个)
Walter Roberson
2023-1-25
Put the channels into a cell array:
lena_ch = num2cell(lena, [1 2]);
Now you can loop over the rows of
indices = perms(1:3)
numrows = size(indices,1); %6 rows
using each row as indices into lena_ch
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!