Plot a vector as a row of grayscale squares
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (3)
Azzi Abdelmalek
le 17 Jan 2014
Modifié(e) : Azzi Abdelmalek
le 17 Jan 2014
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 commentaires
Image Analyst
le 17 Jan 2014
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 commentaires
Voir également
Catégories
En savoir plus sur Scatter Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!