フィルターのクリア

Matrix from vectors (dynamic number of columns)

1 回表示 (過去 30 日間)
Ferial Assmani
Ferial Assmani 2016 年 3 月 26 日
コメント済み: Ferial Assmani 2016 年 3 月 27 日
Dear Matlab users I want to create a matrix form vectors V1, V2 and V3 (same number of rows), number of rows equal to number of rows in vectors) but the number of column is dynamic (relative to an integer called Nbr), let explain with an example:
V1 = [1 2 3]
V1 = [4 5 6]
V1 = [7 8 9]
t = [0.1 0.2 0.3] % (used for an future plot)
If Nbr = 1 the matrix came
1
2
3
I plot (t, V1), the legend is Case1
If Nbr = 2 the matrix came
1 4
2 5
3 6
I plot (t, V1, t, V2), the legend is (Case1, Case2)
If Nbr = 3 the matrix came
1 4 7
2 5 8
3 6 9
I plot (t, V1, t, V2, t, V3), the legend is (Case1, Case2, Case3)
I attached the plot for the last case (Nbr = 3)

回答 (2 件)

Stephen23
Stephen23 2016 年 3 月 26 日
編集済み: Stephen23 2016 年 3 月 26 日
Putting all of your data into separate variables is a really bad idea, so the first thing to do is to save them in one matrix (I called it M). Then your whole task becomes trivial using basic MATLAB indexing, and you can also supply the whole matrix (or part of it) to plot instead of supplying separate input arguments. Much easier!
t = [0.1,0.2,0.3];
M = [1,2,3;4,5,6;7,8,9]; % your data
% create the legend strings:
C = arrayfun(@num2str,1:size(M,1),'uni',0);
C = strcat({'Case '},C);
% pick how many lines to plot:
N = 2;
plot(t,M(1:N,:).')
legend(C(1:N))
See how easy MATLAB can be? It creates this plot:
EDIT: without the legend, to show how the matrix is plotted without wasting time using loops:
t = [0.1,0.2,0.3];
M = [1,2,3;4,5,6;7,8,9];
N = 2;
plot(t,M(1:N,:).')
  7 件のコメント
Stephen23
Stephen23 2016 年 3 月 26 日
編集済み: Stephen23 2016 年 3 月 26 日
The plot function is well documented: "The plot function plots columns of Y versus columns of X".
"Since the OP seems to be new to Matlab" I am going to show them neat and efficient ways of using MATLAB, so that they can proceed to efficiently handling larger and more complicated tasks. That is what I try to do.
Ferial Assmani
Ferial Assmani 2016 年 3 月 26 日
Sorry for my bad questions, but the source of my Data is not a open source, so i must adapt my reading to it, i resume my traitement into the following points :
  1. i have a number of txt files (number is for loop) in each one, i read a matrix, for each position (number is for loop) i read a matrix (ratio and time);
  2. i extract the maximum of ration (his correspondent time) and it position (called interest position)
  3. i extract the pair (ratio, time) of all interest position in all case
  4. i plot the pair (ratio, time) of all interest position in all case
Source of data
Traitement
Plot

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


Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 26 日
編集済み: Azzi Abdelmalek 2016 年 3 月 26 日
v=[1 2 3;4 5 6;7 8 9]
t = [0.1 0.2 0.3]
n=2
plot(t,v(:,1:n)')
legend(genvarname(repmat({'Case'},1,n),'Case'))
  2 件のコメント
Ferial Assmani
Ferial Assmani 2016 年 3 月 26 日
Sorry, a read the vector form a txt file (a previous traitement) so i haven't the matrix i ful it from the vector, so i cut some elements to ful rows, next rows, to next
Ferial Assmani
Ferial Assmani 2016 年 3 月 27 日
Dear Expert: i have tried your guidelines it work except the plot looping:
clear all; clc; close all;
%
V_Time = [1, 2, 3]; % Time Vector fixe length
%
V_Data = 1:1:12; % Data Vector
Nbr_Position = 2; % Nbr of position
Nbr_Case = 2; % Nbr of cases
%
N_Rows= length(V_Time); % Nbr of rows for matrix
N_columns= length(V_Data)/N_Rows; % Nbr of columns for matrix
M_Brut= (reshape(V_Data,[N_Rows,N_columns])); % Brut matrix from Data Vector
%
% in nexte i trim the Brut matrix to forme the paire (Time, Data) for all
% case for all poisition
% -------------------------------------------------------------------------
% Position 1 | .. to .. (i Position)
%--------------------------------------------------------------------------
% Case1 | .. to .. (j Case)| Case 1 | .. to .. (j Case)
%--------------------------------------------------------------------------
% And i plot ( Time Data(case1, ... case(j)) for all position
%
for i=1:Nbr_Position % loop by position
M_Nett{i} = M_Brut(:,(i):(Nbr_Position):(end)); % Nett matrix
for j=1:Nbr_Case % loop by case
plot(V_Time,M_Nett{i}(:,1:j)') % plot pair
title(['Graphes for Node ' num2str(i)]) % title
legend(genvarname(repmat({'Case'},1,j),'Case')) % legend
end
end
%
% Listing Data
% Brut Data
%--------------------------------------------------------
% Case1 | Case2 |
% -------------------------------------------------------
% Position 1 | Position 2 | Position 1 | Position 2 |
%--------------------------------------------------------
% 1 | 4 | 7 | 10 |
% 2 | 5 | 8 | 11 |
% 3 | 6 | 9 | 12 |
%--------------------------------------------------------
% Nette Data to plot
%--------------------------------------------------------
% Position 1 | Position 2 |
% -------------------------------------------------------
% Case1 | Case2 | Case1 | Case2 |
%--------------------------------------------------------
% Time | Data | Time | Data | Time | Data | Time | Data |
%--------------------------------------------------------
% 1 | 1 | 1 | 4 | 1 | 7 | 1 | 10 |
% 2 | 2 | 2 | 5 | 2 | 8 | 2 | 11 |
% 3 | 3 | 3 | 6 | 3 | 9 | 3 | 12 |
%--------------------------------------------------------
Thank

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by