Loop putting information into a new variable matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
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';
0 个评论
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.
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!