Loop putting information into a new variable matrix

조회 수: 1 (최근 30일)
Jessica
Jessica 2014년 6월 23일
답변: Star Strider 2014년 6월 23일
Problem: Assigning information into new variables using an if statement.
channel_number is a nx1 array that could have any value of 1,2,3,4,5. If overall there are three channels then it will have a pattern of 1,2,3,1,2,3,1,2,3,...etc. if it has 5 channels it will be 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...etc.
pixel_info is a nx1 array where n is the same as in channel_number. It contains pixel information for images.
I want to make matrices based on the channel number that contains the pixel information
x=1;
while x<length(channel_number)+1
if channel_number(x)==1
channel1=pixel_info(x);
elseif channel_number(x)==2
channel2=pixel_info(x);
elseif channel_number(x)==3
channel3=pixel_info(x);
elseif channel_number(x)==4
channel4=pixel_info(x);
elseif channel_number(x)==5
channel5=pixel_info(x);
else
disp('error');
end
x=x+1
end
I tried creating matrixes for the channels, which kind of gave me what I wanted. Except it would give me a value and an empty cell and a value and an empty cell. Also it was a horizontal array. I would prefer a vertical one.
x=1;
while x<length(channel_number)+1
if channel_number(x)==1
channel1(x)=pixel_info(x);
elseif channel_number(x)==2
channel2(x)=pixel_info(x);
elseif channel_number(x)==3
channel3(x)=pixel_info(x);
elseif channel_number(x)==4
channel4(x)=pixel_info(x);
elseif channel_number(x)==5
channel5(x)=pixel_info(x);
else
disp('error');
end
x=x+1
end
So overall, I'm just looking for a way to read information into new variables for image analysis.
Any help is greatly appreciated

답변 (2개)

Ben11
Ben11 2014년 6월 23일
Since pixel_info is a cell array you might want to use curly braces {} to access the information it contains, so you would not get a cell array as output. Also if you want a vertical array you can take the transpose:
pixel_info = pixel_info';

Star Strider
Star Strider 2014년 6월 23일
One way to do this is to create empty vectors at the outset and then simply concatenate new values onto them:
channel1 = [];
channel2 = [];
channel3 = [];
channel4 = [];
channel5 = [];
x = 1;
while x<length(channel_number)+1
if channel_number(x)==1
channel1 = [channel1; pixel_info(x)];
elseif channel_number(x)==2
channel2 = [channel2; pixel_info(x)];
...
end
This also creates column vectors out of them.

카테고리

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