How can I simplify my code using loops?

조회 수: 1 (최근 30일)
Changju
Changju 2023년 1월 24일
답변: Walter Roberson 2023년 1월 25일
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);

답변 (1개)

Walter Roberson
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by