Loop putting information into a new variable matrix
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Ben11
on 23 Jun 2014
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';
0 Comments
Star Strider
on 23 Jun 2014
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.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!