Plot a vector as a row of grayscale squares
조회 수: 4 (최근 30일)
이전 댓글 표시
I have a vector (1xN) where the values range from 0 to 1, and I'd like to transform this to a row of grayscale squares (1xN). The colour of each square should range from white (0) to black (1) depending on it's corresponding value in the original vector.
Any ideas on how to do this?
댓글 수: 0
답변 (3개)
Azzi Abdelmalek
2014년 1월 17일
편집: Azzi Abdelmalek
2014년 1월 17일
N=10;
x=rand(1,N)
for k=1:N
x0=k;
y0=0;
x1=x0+1;
y1=1;
rectangle('Position',[x0 y0 x1 y1],'FaceColor',x(k)*[1 1 1],'EdgeColor','k');
hold on;
end
xlim([1 x1])
댓글 수: 0
Image Analyst
2014년 1월 17일
If it's 1xN, it's not square is it? What size do you want the "squares"? If your vector is [0 0 1 1 0 0 1 0 1 1 1 0 0 0 1] or similar then you can't get squares out of that because the first stretch of 0 would be a 2x2, then the [1 1] would also be a 2x2 square, but the 1 and the 0 are only 1x1 if they're square, and the 1 1 1 would turn into a 3x3 square, not a 2x2 square. If all your stretches of 1's and 0's are the same, you can just use repmat() to replicate the row vector vertically once you know how big the stretches are
% Create Sample data
rowVector = [0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1]
% Label the separate regions.
labeledRegions = bwlabel(rowVector);
% Measure the area of the regions.
measurements = regionprops(labeledRegions, 'Area');
% Find the longest stretch of 1's.
squareSide = max([measurements.Area])
% Replicate to make a square array out of each region.
squareArray = repmat(rowVector, [squareSide, 1])
In the command window:
rowVector =
0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1
squareSide =
3
squareArray =
0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1
0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1
0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1
Sorry it doesn't look good but each stretch of 3 1's is now a 3x3 block of 1's in a 3 x N array.
There is a checkerboard() function you know. Maybe you are just looking for that?
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!