简体   繁体   中英

Matlab - Creating a vector from matrices

I have some color matrices like below and originally, there is only one color matrix and it works well.

[66 92 143]/255; %DARKBLUE 
[21 59 99]/255; %BLACK
[0 0 0]; %Pure black

tempPoint.set('mark_color',{[21 59 99]/255});

I have tried to put these color matrices into a vector.So I can use it in a loop like this:

farbe=[[21 59 99]/255 [0 0 0] [66 92 143]/255];

 for i=1:length(farbe) 

tempPoint.set('mark_color',{farbe(i)}); 

end

But unfortunately it doesn't work and it gives an " Color value must be a 3 element numeric vector" error

I tried to find a solution to my problem at this topic but couldn't make it work for mine:

Array of Matrices in MATLAB

How can I put these matrices into a vector and use its every element in a loop ?

Any help will be appreciated.

Your Problem is: your new farbe has size 9. therefor in your loop you are calling it with only 1 input instead of 3. Either change your parameter in farbe or change the datatype. Here 2 solutions:
1 solution: Using cell:

%I changed farbe to be a cell array with each element containing 1 colour
farbe={[21 59 99]/255, [0 0 0], [66 92 143]/255};

 for i=1:length(farbe) 
  %Here the call farbe changed (using {} instead of() to get the values)
  tempPoint.set('mark_color',{farbe{i}}); 

end

2nd solution:other looping

farbe=[[21 59 99]/255 [0 0 0] [66 92 143]/255];

 for i=1:length(farbe)/3 

tempPoint.set('mark_color',{farbe(3*i-2:3*i)}); 

end

Here the diferent parameter is to make sure that you use right indexing. You could also change your loop parameter to i=1:3:7 and use farbe(i:i+2)

Also as an annotation you shouldn't use i as parameter since it is also the MATLAB intern variable for imaginary units. same as j. Use either ii and jj or something else.

Also i wasn't able to test my solutions since i don't have a temp.Point.set method. So feedback would be appreciated.

You can do this in two ways either by making farbe into a matrix like this:

farbe=[[21 ;59 ;99]/255 [0 ;0 ;0] [66; 92 ;143]/255]

and then

for i=1:length(farbe) 

tempPoint.set('mark_color',{farbe(:,i)}); 

end

or make them as a cell by:

farbe=[{[21 59 99]/255} {[0 0 0]} {[66 92 143]/255}];

for i=1:length(farbe) 

tempPoint.set('mark_color',{farbe{i}}); 

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM