# Problem working with 3-D image matrix

9 views (last 30 days)
Mardhika Jenned on 4 Mar 2011
I want to create an image is divided into 3 parts automatically with different levels of light, but this script does not work for an image that has a decimal value after a divided three-dimensional, why ?
this is my script.
g=input('input picture name(ex : xxxx.jpg or xxxx.gif"): ','s');
start=imread(g);
s=size(start);
z=s(1,1);
a=z/3;
y=ceil(a);
x=y+1;
b=y*2;
c=y*3;
start([1:y],:,:)=uint8(double(start([1:y],:,:))*4);
start([y:b],:,:)=uint8(double(start([y:b],:,:))*1);
start([b:c],:,:)=uint8(double(start([b:c],:,:))*0);
image(start);
error says
??? Index exceeds matrix dimensions.
Error in ==> kaka at 12
start([b:c],:,:)=uint8(double(start([b:c],:,:))*0);
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Brett Shoelson on 4 Mar 2011
I'm not sure what you mean by "does not work for an image that has a decimal value after a divided three-dimensional."
But I think that you're trying to do something like this:
start = imread(g);
base = ones(ceil(size(start,1)/3),size(start,2),size(start,3));
base = uint8(cat(1,base*4,base,base*0));
start = start.*base;
Cheers, Brett
##### 1 CommentShow -1 older commentsHide -1 older comments
Mardhika Jenned on 4 Mar 2011
please use the formula for a beginner like I use, because I do not understand the formula you provide. .
your formula is doesn't work for picture it has vertical size decimal,
example: vertical size is 101
100 /3 = 33.33333333 <<< the program is error
please help me !

Sign in to comment.

### More Answers (2)

Brett Shoelson on 4 Mar 2011
Well, if you want to chop your image into thirds and it doesn't divide equally into three bins, then YOU have to decide how to chop it. CEIL, ROUND, FLOOR, FIX...all convert fractions to whole numbers.
If you want to ensure that your matrix dimensions match, thry this:
start = imread(g);
base = ones(ceil(size(start,1)/3),size(start,2),size(start,3));
base = uint8(cat(1,base*4,base,base*0));
base = base(1:size(start,1),:,:);
start = start.*base;
So, I first built a matrix of ones 1/3 the size of start. Then I concatenated (CAT) three copies of it, once multiplied by 4, once multiplied by 1, and once multiplied by zero. That will either be exactly the same size as start, or 1 or 2 rows longer than start. So next, I "cropped" (base = base(1:size(start,1),:,:);) the result to be the same size as start, and then multiplied the two matrices element-by-element.
Cheers, Brett
##### 2 CommentsShow NoneHide None
Mardhika Jenned on 4 Mar 2011
can you use the matriks logic to solve this problem ? thx
Walter Roberson on 4 Mar 2011
What do you include in "matrix logic" ?
If you mean something like matrix multiplication, then NO. Brett's code will expand the matrix by up to two rows, but matrix multiplication cannot change the number of rows in a matrix.

Sign in to comment.

Brett Shoelson on 5 Mar 2011
Here's another approach, just for fun:
start = imread(g);
multCol = reshape(repmat([4 1 0],ceil(size(start,1)/3),1),[],1);
multCol = uint8(multCol(1:size(start,1),:));
start = bsxfun(@times,start,multCol);
Have a good weekend!
Brett
##### 1 CommentShow -1 older commentsHide -1 older comments
Brett Shoelson on 5 Mar 2011
Or, more generally, change that second "multCol=" line to:
multCol = cast(multCol(1:size(start,1),:),class(start));
Walter's right, of course, that your matrix multiplications have to make mathematical sense, dimensionally speaking. For element-wise multiplications (.*), for instance, the matrices have to be the same size. But if the matrices you are attempting to multiply differ dimensionally in such a way that expanding singleton dimensions can make the matrices dimensionally consistent, the underused BSXFUN allows you to play fast and loose with your matrices! :)
Cheers,
Brett

Sign in to comment.

### Categories

Find more on Matrices and Arrays in Help Center and File Exchange

### Tags

No tags entered yet.

### Community Treasure Hunt

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

Start Hunting!