Azzera filtri
Azzera filtri

Accessing a vector form a matrix

2 visualizzazioni (ultimi 30 giorni)
Luke
Luke il 22 Apr 2024
Commentato: Stephen23 il 22 Apr 2024
Hi, I am writing a solver scipt where I will have outputed 3 vectors called x1, x2, and x3. I then plan to turn these into a 3 x (some value tbd) vector, meaning x=[x1, x2, x3].
In the main script I need to plot x1, x2 and x3 independently, which brings me to my question. How do I call, for instance, the x1 vector from the 'x' matrix so I can plot it please?

Risposte (2)

John D'Errico
John D'Errico il 22 Apr 2024
Modificato: John D'Errico il 22 Apr 2024
As an example, given vectors x1,x2,x3. I'll make some up.
x1 = randi(10,1,5)
x1 = 1x5
7 8 6 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x2 = randi(10,1,5)
x2 = 1x5
9 5 5 6 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x3 = randi(10,1,5)
x3 = 1x5
2 3 10 10 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Combine them into an array, here of size 3x5.
x = [x1;x2;x3]
x = 3x5
7 8 6 4 1 9 5 5 6 3 2 3 10 10 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can access the original x1 as:
x(1,:)
ans = 1x5
7 8 6 4 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Hassaan
Hassaan il 22 Apr 2024
Modificato: Hassaan il 22 Apr 2024
% Dummy data for vectors x1, x2, and x3
x1 = sin(linspace(0, 2*pi, 100)); % Example data for x1 (a sine wave)
x2 = cos(linspace(0, 2*pi, 100)); % Example data for x2 (a cosine wave)
x3 = linspace(0, 1, 100); % Example data for x3 (a linear increase)
% Combine x1, x2, and x3 into a single matrix x
x = [x1; x2; x3]';
% Extract each vector from the matrix x
extracted_x1 = x(:, 1); % This extracts the first column from matrix x, which is vector x1
extracted_x2 = x(:, 2); % This extracts the second column, which is vector x2
extracted_x3 = x(:, 3); % This extracts the third column, which is vector x3
% Plot each vector individually
% Plot for x1
figure; % Open a new figure window for x1
plot(extracted_x1, 'b-', 'LineWidth', 2); % Plot x1 with a blue line
title('Plot of x1');
xlabel('Index');
ylabel('Value');
% Plot for x2
figure; % Open a new figure window for x2
plot(extracted_x2, 'r-', 'LineWidth', 2); % Plot x2 with a red line
title('Plot of x2');
xlabel('Index');
ylabel('Value');
% Plot for x3
figure; % Open a new figure window for x3
plot(extracted_x3, 'g-', 'LineWidth', 2); % Plot x3 with a green line
title('Plot of x3');
xlabel('Index');
ylabel('Value');
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by