How can I create a multiple fill with different colors using loops for an arbitrary vector

7 ビュー (過去 30 日間)
Hi all, I am not very advance in matlab so forgive me if I mix up the terms. I understand the basics of how to use the fill function fill(X,Y,C). From matlab documentation, it can also be used for multiple fills say fill(X1,Y1,C1,X2,Y2,C2,...).
I have a vector
X = [1 2 2 2 4 4 5 5 3 3 3 1 1 1]
and
Y = [0 1 1 0 0]
Nz = linspace(0,1,length(X))
dz = max(Nz)/length(X)
I want to write a for loop to create a multiple fill for each identical successive element with different colors for example
x1 = [0 dz*count dz*count 0 0]
Y = [0 1 1 0 0]
x2 = [dz*count dz*count2 dz*count2 dz*count dz*count]...
Y = [0 1 1 0 0]
C1,C2...
fill(x1,Y,C1,x2,Y,C2...).
"count" here determines the number of times successive elements occur in the vector. "dz" is the step size. I would be glad if anyone here can assist. Thank you.
  3 件のコメント
Lewis Asilevi
Lewis Asilevi 2018 年 1 月 12 日
Thank you for your response Guillaume. Sorry for not been clear enough. Here, I want to scale the elements of X on Nz. So for the first element of X that is 1, it has a scaled value of 1*dz = 0.0714. the 1 multiplying dz is just the count of 1 until 2. Similary, for the second element of X that is 2, it occurs three times in succession so we have 3*dz = 0.214 and for the fifth element of X that is 4, it occurs twice in succession so we have 2*dz = 0.1428 and it continous in that fashion. So in this case, x1 = [0 1*dz 1*dz 0 0], x2=[1*dz 3*dz 1*dz 1*dz]. It continuous in that fashion until the last successive elements. So in the end we ought to have something like as shown in the image. where the width of each color in the Nz direction coresponds to the calculations above and the color determined from the elements of X itself( for example the first and last elements of X are the same (1) so they have the same color). I hope this helps clarify the question.
in the image.
Lewis Asilevi
Lewis Asilevi 2018 年 1 月 12 日
And the goal is to be able to write a function that can handle an arbitray vector array in the same manner

サインインしてコメントする。

採用された回答

Lewis Asilevi
Lewis Asilevi 2018 年 1 月 13 日
Hello, Guillaume, I want to thank you so much for your assistance throughtout this thread. It has been really invaluable. Also thank you Stephen Cobeldick. With regards to the last question, with some experimentation, I was able to use a for loop to loop through like so
fill(xpoints(:,ii), ypoints(:,ii),colormap(colour(ii,:)))
and got what I was looking for. Thank you very much.

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 1 月 12 日
編集済み: Guillaume 2018 年 1 月 12 日
As suspected a loop is not required:
X = [1 2 2 2 4 4 5 5 3 3 3 1 1 1];
seqlengths = diff([0, find(diff(X)), numel(X)]); %calculate the length of each continuous sequence
colour = X(cumsum(seqlengths)); %extract the colour corresponding to each sequence
xpoints = repelem(cumsum([0 seqlengths(1:end-1); seqlengths], 2), 2, 1); %build xpoints matrix
ypoints = [0 1 1 0].';
fill(xpoints, ypoints, colour);
But as pointed out by Stephen, using image is even simpler.
  3 件のコメント
Guillaume
Guillaume 2018 年 1 月 12 日
The colour parameter is unchanged from your original code. It is an index into the colour map. You can use any colour map you wish.
You can multiply the xpoints coordinates by any factor you want.
xpoints = xpoints / numel(X) %to scale x axis to [0 1]
In order to understand better the code I've written, I would recommend you break it down and see what each function does. For example
  • diff(X) calculates the difference between consecutive elements of X. It's therefore going to be 0 for consecutive identical elements and non-zero otherwise.
  • find(diff(X)) thus returns the indices at which the X values change
  • the diff of that is thus the length of each sequence
  • etc.
Lewis Asilevi
Lewis Asilevi 2018 年 1 月 13 日
編集済み: Lewis Asilevi 2018 年 1 月 13 日
Hello Guillaume, thank you for your help so far. Correct me if I am wrong. If I have n number of xpoints, to define a custom colormap, they should also be C1,C2...Cn. In our case, we have 6 xpoints right?. I define a custom colormap set as in the matrice below. colour = [ 1.0000 1.0000 1.0000; 0.5918 0.5918 0.5918; 0.7113 0.7113 0.7113; 0.5000 0.5000 0.5000; 0.2778 0.2778 0.2778; 0.4444 0.4444 0.4444 ] When I call it as fill(xpoints, ypoints,colour), I get the error, Error using fill Vectors must be the same length. What am I doing wrong?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBlue についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by