Filling an array with color points

2 次查看(过去 30 天)
I am trying to create an array (matrix?)
I have J curves I am plotting and I want to create a vector of color values.
The matrix will have J rows and 3 entries per row. the First entry will be 0 the third entry will be 1. The second middle entry will be (i-1)*1./J. where i is my counter in the for loop.
```
j = 34; g = 3;
A = zeros(j,g);
for i= 1:j
A(:,i) = 0 (i-1)*1./j 1;
end
disp(A)
```
So I want something like, if j=34 , A=[0 0 1; 0 1/34 1; 0 2/34 1; ... ; 0 33/34 1;]
Thank you for your time and help.

采纳的回答

Les Beckham
Les Beckham 2022-4-1
编辑:Les Beckham 2022-4-1
j = 34; g = 3;
A = zeros(j,g);
A(:,3) = 1; % put ones in the third column
A(:,2) = ([0:j-1]/j).'; % fill in the second column (' makes it a column)
format rat
disp(A)
0 0 1 0 1/34 1 0 1/17 1 0 3/34 1 0 2/17 1 0 5/34 1 0 3/17 1 0 7/34 1 0 4/17 1 0 9/34 1 0 5/17 1 0 11/34 1 0 6/17 1 0 13/34 1 0 7/17 1 0 15/34 1 0 8/17 1 0 1/2 1 0 9/17 1 0 19/34 1 0 10/17 1 0 21/34 1 0 11/17 1 0 23/34 1 0 12/17 1 0 25/34 1 0 13/17 1 0 27/34 1 0 14/17 1 0 29/34 1 0 15/17 1 0 31/34 1 0 16/17 1 0 33/34 1

更多回答(1 个)

Voss
Voss 2022-4-1
I think this is what you were going for:
j = 34; g = 3;
A = zeros(j,g);
for i= 1:j
A(i,:) = [0 (i-1)/j 1];
end
disp(A)
0 0 1.0000 0 0.0294 1.0000 0 0.0588 1.0000 0 0.0882 1.0000 0 0.1176 1.0000 0 0.1471 1.0000 0 0.1765 1.0000 0 0.2059 1.0000 0 0.2353 1.0000 0 0.2647 1.0000 0 0.2941 1.0000 0 0.3235 1.0000 0 0.3529 1.0000 0 0.3824 1.0000 0 0.4118 1.0000 0 0.4412 1.0000 0 0.4706 1.0000 0 0.5000 1.0000 0 0.5294 1.0000 0 0.5588 1.0000 0 0.5882 1.0000 0 0.6176 1.0000 0 0.6471 1.0000 0 0.6765 1.0000 0 0.7059 1.0000 0 0.7353 1.0000 0 0.7647 1.0000 0 0.7941 1.0000 0 0.8235 1.0000 0 0.8529 1.0000 0 0.8824 1.0000 0 0.9118 1.0000 0 0.9412 1.0000 0 0.9706 1.0000
And you can do it without the loop like this:
A = [zeros(j,1) (0:j-1).'/j ones(j,1)];
disp(A)
0 0 1.0000 0 0.0294 1.0000 0 0.0588 1.0000 0 0.0882 1.0000 0 0.1176 1.0000 0 0.1471 1.0000 0 0.1765 1.0000 0 0.2059 1.0000 0 0.2353 1.0000 0 0.2647 1.0000 0 0.2941 1.0000 0 0.3235 1.0000 0 0.3529 1.0000 0 0.3824 1.0000 0 0.4118 1.0000 0 0.4412 1.0000 0 0.4706 1.0000 0 0.5000 1.0000 0 0.5294 1.0000 0 0.5588 1.0000 0 0.5882 1.0000 0 0.6176 1.0000 0 0.6471 1.0000 0 0.6765 1.0000 0 0.7059 1.0000 0 0.7353 1.0000 0 0.7647 1.0000 0 0.7941 1.0000 0 0.8235 1.0000 0 0.8529 1.0000 0 0.8824 1.0000 0 0.9118 1.0000 0 0.9412 1.0000 0 0.9706 1.0000

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by