Image Processing: Array Problem - right justify?!

1 view (last 30 days)
I'm attempting to build an array of pixel values that correspond to the pixel values found to the left of a particular pixel of interest. This is an iterative process.
To keep it simple, I will illustrate this for 2 pixels only. The user may ask to collect 5 pixels to the left of a particular coordinate, so in the first run, the entry might look like this:
array =
123 122 117 120 120
0 0 0 0 0
However, in the second run, the next coordinate of interest happened to be too close to the left edge of the image, and so only 3 values could be collected:
array =
123 122 117 120 120
120 124 122 0 0
The problem is that the pixel values should actually read:
array =
123 122 117 120 120
0 0 120 124 122
since they were found directly left of the pixel of interest as in the first run... it just so happened that we could not get the first and second values as these didn't exist.
Does anyone know of a way that I can insert this data to the right of the row as shown? Or would I have to figure out how many pixels were missing (in this example, 2), and then shift the row right by that amount?
Finally, at the end of this loop I will calculate a column-wise average of this data. Since the zeros at the beginning of the line actually indicate "missing data", I would prefer to omit these from the averaging process altogether. So might be something like:
123 / 1
instead of:
123 + 0 / 2
How can I nullify these entries such that they will be skipped in the averaging process? I have tried NaN and [ ] to create empty cells, but it doesn't seem to do the trick.
Thanks!!

Accepted Answer

Sean de Wolski
Sean de Wolski on 13 May 2011
Part 1:
array(2,:) = fliplr(array(2,:))
Part 2:
array(array==0) = nan;
colmean = nanmean(array,1);
  3 Comments
Philip
Philip on 13 May 2011
Thanks for your help!
I have tried "fliplr" but it seems to literally flip the data, so 1 2 3 becomes 3 2 1 instead of just shifting it along... Am I doing something wrong? I checked the help files, but couldn't see an example close to mine.
The part 2 answer looks great though, thanks!

Sign in to comment.

More Answers (2)

Leah
Leah on 13 May 2011
array(array==0)=NaN
are you planning on using nanmean?

Teja Muppirala
Teja Muppirala on 13 May 2011
For the first part, you can use the keyword "end"
A = zeros(5);
A(1,end) = 4;
A(2,end-2:end) = [1 2 3];
A(3,end-4:end) = [2 4 6 8 10];
A(4,end-1:end) = [8 9];
A(5,end-3:end) = 7
(There is also a command FLIPLR, that you could use to accomplish this same kind of thing)
For the second part, you can do this:
sum(A)./sum(A~=0)

Community Treasure Hunt

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

Start Hunting!