Asked by Bubbles Lol
on 9 Sep 2019

function [r,g,b] = Distant_Pixel(n)

pixels= gallery('integerdata',54,[1,n,3],42);

[R,G,B] = Median_Pixel(n);

r = Pixel_Distance([R,G,B],[pixels(1,1),pixels(1,1,2),pixels(1,1,3)])

g = Pixel_Distance([R,G,B],[pixels(1,2),pixels(1,2,2),pixels(1,2,3)])

b = Pixel_Distance([R,G,B],[pixels(1,3),pixels(1,3,2),pixels(1,3,3)])

end

Median_Pixel and Pixel_Distance are functions.

Since n will change depending on input. How do I fix the pixels matrix so that it works with any input of n

[pixels(1,1),pixels(1,1,2),pixels(1,1,3)] -- ?

Answer by Walter Roberson
on 9 Sep 2019

function [r,g,b] = Distant_Pixel(n)

pixels= gallery('integerdata',54,[1,n,3],42);

[R,G,B] = Median_Pixel(n);

r = []; g = []; b = [];

if n >= 1

r = Pixel_Distance([R,G,B],[pixels(1,1),pixels(1,1,2),pixels(1,1,3)]);

end

if n >= 2

g = Pixel_Distance([R,G,B],[pixels(1,2),pixels(1,2,2),pixels(1,2,3)]);

end

if n >= 3

b = Pixel_Distance([R,G,B],[pixels(1,3),pixels(1,3,2),pixels(1,3,3)])

end

end

Bubbles Lol
on 9 Sep 2019

Walter Roberson
on 9 Sep 2019

We do not know what your function Pixel_Distance does.

It is not clear what the most "distance" rgb values means in this situation, since you appear to be talking about 3 different values

Given a set of pixel values, you can take the mean or median . Now for any one median component (say the G), check to see whether the component is above or below 1/2 of the maximum possible value (e.g., 128 if the values are uint8). If the component is below 1/2 of the maximum possible, then the most distant value is the maximum possible value (e.g., 255); if the component is above 1/2 of the maximum possible, then the most distant value is the minimum possible value (e.g., 0)

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479495-looping-through-a-matrix-with-unknown-dimensions#comment_743717

## Bubbles Lol (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479495-looping-through-a-matrix-with-unknown-dimensions#comment_743737

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479495-looping-through-a-matrix-with-unknown-dimensions#comment_743745

## Bubbles Lol (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479495-looping-through-a-matrix-with-unknown-dimensions#comment_743751

## Rik (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479495-looping-through-a-matrix-with-unknown-dimensions#comment_744645

## John D'Errico (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479495-looping-through-a-matrix-with-unknown-dimensions#comment_744648

## Rena Berman (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479495-looping-through-a-matrix-with-unknown-dimensions#comment_747750

Sign in to comment.