Vector magnitude with colors (2024)

65 views (last 30 days)

Show older comments

JACK LONDON on 31 Dec 2022

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors

Commented: Image Analyst on 1 Jan 2023

Accepted Answer: Voss

  • vector3dn.txt

Open in MATLAB Online

I want to print velocity vectors on Matlab. I use function "quiver".

Vector magnitude with colors (2)

It satisfies to draw velocity vectors but I can t give color to the vectors.

I try to give color to velocity vectors depend on magnitude of vectors like this image

Vector magnitude with colors (3)

Which code I need to color velocity vectors with colorbar like above image? Thank you.

This is my codes:

xyz=dlmread('vector3dn.txt');

X=xyz(:,1);

Y=xyz(:,2);

Z=xyz(:,3);

U=xyz(:,4);

V=xyz(:,5);

W=xyz(:,6);

quiver3(X,Y,Z,U,V,W)

axis equal

Note : I uploaded my source file as an attachment.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Voss on 31 Dec 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#answer_1139152

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#answer_1139152

Edited: Voss on 31 Dec 2022

  • vector3dn.txt

As far as I can tell, there is no way to create a quiver plot (or quiver3 plot) with quivers of different colors.

One way to approach this problem is to create one quiver3 plot (of a given color) for each color in the colormap (i.e., the colors depicted in the colorbar). Now, in quiver/3 the quivers are scaled in length so as not to overlap each other (with an additional optional scale factor), but the only way (as far as I can tell) to get multiple quiver/3 plots to use the same scale factor is to turn that scaling off. So that could work, scaling the magnitude data down by an appropriate amount before sending it to quiver/3:

% xyz=dlmread('vector3dn.txt');

xyz = readmatrix('vector3dn.txt');

% scale the u,v,w components of the vectors down, for nice plotting

sf = 0.1;

xyz(:,4:6) = xyz(:,4:6)*sf;

% calculate the magnitudes of the vectors

magnitude = sqrt(sum(xyz(:,4:6).^2,2));

% calculate the (rounded) min and max magnitude

mag_res = 0.05;

mlim = [ ...

floor(min(magnitude)/mag_res) ...

ceil(max(magnitude)/mag_res) ...

]*mag_res;

% define a color map

n_colors = 64;

cmap = autumn(n_colors);

% calculate the magnitude thresholds, between each pair of which the

% quivers will be one color

mthresholds = linspace(mlim(1),mlim(2),n_colors+1);

% create a figure, and go ahead and hold on for multiple plots

figure

hold on

% for each color

for ii = 1:n_colors

% find the indicies of the magnitudes at this color level

idx = magnitude >= mthresholds(ii) & magnitude < mthresholds(ii+1);

% construct the x,y,z,u,v,w arguments for quiver3 corresponding

% to those magnitudes within this level

args = num2cell(xyz(idx,:),1);

% create the quiver3 plot of the right color, with no auto-scaling

quiver3(args{:},'off','Color',cmap(ii,:))

end

% set some axes properties

box on

grid on

axis equal

view(3);

set(gca(),'Color','k')

% set up the colorbar

colormap(cmap);

colorbar();

caxis(mlim/sf);

Vector magnitude with colors (5)

Or you can use line objects instead of quivers; the approach is the same - splitting it up into multiple sets of lines, one for each color. You just have to calculate the "other" end of each line (x+u, y+v, z+w).

% xyz=dlmread('vector3dn.txt');

xyz = readmatrix('vector3dn.txt');

% calculate the magnitudes of the vectors

magnitude = sqrt(sum(xyz(:,4:6).^2,2));

% calculate the (rounded) min and max magnitude

mag_res = 0.05;

mlim = [ ...

floor(min(magnitude)/mag_res) ...

ceil(max(magnitude)/mag_res) ...

]*mag_res;

% define a color map:

n_colors = 64;

cmap = autumn(n_colors);

% calculate the magnitude thresholds, between each pair of which the

% lines will be one color

mthresholds = linspace(mlim(1),mlim(2),n_colors+1);

% create a figure

figure

% scale factor, for plotting

% for each color

for ii = 1:n_colors

% find the indicies of the magnitudes at this color level

idx = magnitude >= mthresholds(ii) & magnitude < mthresholds(ii+1);

n = nnz(idx);

% construct the x, y, z coordinates of the lines [x x+u], [y y+v], [z z+w]

x = xyz(idx,1)+[zeros(n,1) sf*xyz(idx,4)];

y = xyz(idx,2)+[zeros(n,1) sf*xyz(idx,5)];

z = xyz(idx,3)+[zeros(n,1) sf*xyz(idx,6)];

% create the lines with the right color

line(x.',y.',z.','Color',cmap(ii,:))

end

% set some axes properties

box on

grid on

axis equal

view(3);

set(gca(),'Color','k')

% set up the colorbar

colormap(cmap);

colorbar();

caxis(mlim);

Vector magnitude with colors (6)

4 Comments

Show 2 older commentsHide 2 older comments

JACK LONDON on 1 Jan 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542162

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542162

Is there a way set backround color as a white instead of black? Thank you.

Voss on 1 Jan 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542172

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542172

Open in MATLAB Online

Sure, just delete this line:

set(gca(),'Color','k')

JACK LONDON on 1 Jan 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542177

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542177

Thank you. As a last question could be range of the colors blue tones instead of yelllow-red colors?

Image Analyst on 1 Jan 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542327

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542327

Open in MATLAB Online

See the colormap documentation for some preset colormaps. You might like winter instead of autumn:

n_colors = 8; % Whatever

cmap = winter(n_colors) % Blue to green

cmap = 8×3

0 0 1.0000 0 0.1429 0.9286 0 0.2857 0.8571 0 0.4286 0.7857 0 0.5714 0.7143 0 0.7143 0.6429 0 0.8571 0.5714 0 1.0000 0.5000

or you can make your own one of pure blues:

n_colors = 8; % Whatever

cmap = [zeros(n_colors, 1), zeros(n_colors, 1), linspace(0, 1, n_colors)'] % Pure blue

cmap = 8×3

0 0 0 0 0 0.1429 0 0 0.2857 0 0 0.4286 0 0 0.5714 0 0 0.7143 0 0 0.8571 0 0 1.0000

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationColormapsRed

Find more on Red in Help Center and File Exchange

Tags

  • vectors
  • matlab

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Vector magnitude with colors (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Vector magnitude with colors (2024)

FAQs

How do you find the magnitude of a vector? ›

Thus, the formula to determine the magnitude of a vector (in two-dimensional space) v = (x, y) is: |v| =√(x2 + y2). This formula is derived from the Pythagorean theorem.

How to find the magnitude of a vector with three components? ›

Answer: The magnitude of a 3-dimensional vector with 3 components V = (a, b, c) is given as √(a2 + b2 + c2). Let's look into the given steps. Explanation: The magnitude of a vector signifies the positive length of a vector.

How is the magnitude of a vector represented in a vector diagram? ›

Vector diagrams are simply diagrams that contain vectors. A vector is an arrow that represents a quantity with both magnitude and direction. The length of the arrow represents the magnitude (or size) of the quantity, and the direction of the arrow represents the direction.

What is the formula for the magnitude of a vector product? ›

The magnitude vector product of two given vectors can be found by taking the product of the magnitudes of the vectors times the sine of the angle between them.

How do you find the magnitude of the resultant force of three vectors? ›

The magnitude of the resultant of two equal vectors is equal to the magnitude of either vector.

How do you find the resultant vector of three components? ›

  1. To find the resultant vector from three vectors given their components in terms of their angles between each other, you can follow these steps:
  2. Rx = Ax + Bx + Cx Ry = Ay + By + Cy Rz = Az + Bz + Cz.
  3. |R| = √(Rx^2 + Ry^2 + Rz^2)
  4. The direction of the resultant vector R can be found using trigonometry.
Apr 19, 2023

How do you find the magnitude of two vectors A and B? ›

MAGNITUDE AND DIRECTION OF A VECTOR

Given a position vector →v=⟨a,b⟩,the magnitude is found by |v|=√a2+b2.

What is the magnitude of two vectors? ›

Hence, the magnitude of the resultant of two vectors, and is given by the square root of A 2 + B 2 + 2 A B cos θ .

How do you find the magnitude of a cross B vector? ›

The Magnitude of Resultant of Cross Product

A × B =|A| |B| sin θ. As a result, the magnitude of A×B is very similar to that of the dot product. if A and B are parallel, the angle between them is zero, so sin=0, and |A×B|=0, similarly, if they are anti-parallel, sinθ=0, and |A×B|=0.

What is a vector in physics for dummies? ›

A vector is a combination of exactly two values: a magnitude (like the speed of an object in motion) and a direction (such as the direction of an object in motion). All kinds of things can be described with vectors, including velocity, acceleration, displacement, magnetic fields, electric fields, and many more.

How do you notate the magnitude of a vector? ›

The magnitude of the vector a is denoted as ∥a∥. See the introduction to vectors for more about the magnitude of a vector.

What is the magnitude of the vector 3i 4j? ›

Therefore the magnitude of the vector 3i +4j, is 5 units. This statement can be mathematically written as: |3i+4j| = 5. (The vertical lines mean magnitude).

What is the formula of magnitude of unit vector? ›

The magnitude of a unit vector sums the individual measures of the vector along the x-axis, y-axis, and z-axis respectively. The magnitude for a given vector i.e P is denoted by |p| and can be achieved by calculating the square root of the sum of the square of its directional ratios.

How do you find the magnitude value? ›

Step 1: The magnitude of a Vector from components is found with the equation: a 2 + b 2 . To use this equation, first, we need to find the sum of the squares of each of the components. Step 2: Take the square root of the value calculated in the previous step. Step 3: Simplify the square root.

How to find the magnitude of the sum of two vectors? ›

This is the addition of vectors formula: Given two vectors a = (a1, a2) and b = (b1, b2), then the vector sum is, M = (a1 + b1, a2 + b2) = (Mx, My). In this case, magnitude of the resultant vector sum M = |M| = √ ((Mx)2+(My)2) and.

Top Articles
MSPA PREMIUM Super Camaro 6 Person Inflatable Hot Tub Spa
MSpa Whirlpool Camaro für 6 Personen | Indoor+Outdoor
Promotional Code For Spades Royale
Live Basketball Scores Flashscore
Fat Hog Prices Today
80 For Brady Showtimes Near Marcus Point Cinema
O'reilly's In Monroe Georgia
Umn Pay Calendar
Irving Hac
[2024] How to watch Sound of Freedom on Hulu
Space Engineers Projector Orientation
Crusader Kings 3 Workshop
Bros Movie Wiki
Citymd West 146Th Urgent Care - Nyc Photos
Marion County Wv Tax Maps
U/Apprenhensive_You8924
Bahsid Mclean Uncensored Photo
Bnsf.com/Workforce Hub
Katherine Croan Ewald
Palm Coast Permits Online
Uktulut Pier Ritual Site
Loves Employee Pay Stub
Ford F-350 Models Trim Levels and Packages
Chase Bank Pensacola Fl
Free Personals Like Craigslist Nh
Chime Ssi Payment 2023
Danielle Ranslow Obituary
Gilchrist Verband - Lumedis - Ihre Schulterspezialisten
480-467-2273
Riverstock Apartments Photos
Allegheny Clinic Primary Care North
Mastering Serpentine Belt Replacement: A Step-by-Step Guide | The Motor Guy
R/Sandiego
Solarmovie Ma
How to Draw a Bubble Letter M in 5 Easy Steps
Chattanooga Booking Report
Uhaul Park Merced
Pinellas Fire Active Calls
Planet Fitness Lebanon Nh
Is Arnold Swansinger Married
Marcus Roberts 1040 Answers
Crazy Balls 3D Racing . Online Games . BrightestGames.com
159R Bus Schedule Pdf
Armageddon Time Showtimes Near Cmx Daytona 12
Ursula Creed Datasheet
Satucket Lectionary
Hk Jockey Club Result
Yale College Confidential 2027
A Snowy Day In Oakland Showtimes Near Maya Pittsburg Cinemas
Festival Gas Rewards Log In
Myhrkohls.con
Ff14 Palebloom Kudzu Cloth
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 5717

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.