How can I simplify my code using loops?

1 Ansicht (letzte 30 Tage)
Changju
Changju am 24 Jan. 2023
Beantwortet: Walter Roberson am 25 Jan. 2023
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);

Antworten (1)

Walter Roberson
Walter Roberson am 25 Jan. 2023
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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by