How to conditionality concat columns of a matrix?

1 回表示 (過去 30 日間)
Andi
Andi 2022 年 3 月 31 日
コメント済み: Stephen23 2022 年 4 月 5 日
Hi everyone,
My data set consists of 60 rows and 86 columns. First row is parameter values that are ranging from 1.2 to 1.73. I required to select columns based on the parameter values. In the below example, I select all the columns having parameter value between 1.2 and 1.3. However, it is a bit hard to compute this manully. May someone help me tyo automate this for a window of 0.5.
For exmaple, same process repeat for 1-1.5, 1.5-2, 2-2.5 and so on. and store mean parametere values in one matrix and selected columns in another matrix.
format short
% Lines below this are not important .........
X = load('PDS_case.csv'); % input data
C_sort = sortrows(X,5);
Tri = C_sort(1:86,:);
data1 = sortrows(Tri,7);
PDS=(data1(:,7));
dd=(PDS*1e8);
data2=data1(:,8:66);
Arr=[dd data2]
A=Arr';
% Lines below this are important ...........
TheArray=A; % Data set after preliminary processing
C=1.0; % lower limit for parameter value
D=1.5; % upper limit of the parametere values
mask = (TheArray(1,:) < D & TheArray(1,:)>C); % Masking condition
output = TheArray(:,mask); % selected array
pds=mean (output(1,:)); % mean of the parameteres velues of the selected coloumns
ev=output(2:end,:); % Delete parametere row from selected data
d1 = ev(:); % matrix converted to column matrix
  1 件のコメント
Stephen23
Stephen23 2022 年 4 月 5 日
"same process repeat for 1-1.5, 1.5-2, 2-2.5 and so on"
Then you need to ignore any badly-written code written using loops to repeat code. That will not make your task easier.
A much better approach would use discretization functions to bin the data, e.g. HISTCOUNTS:

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

採用された回答

Santosh Fatale
Santosh Fatale 2022 年 4 月 5 日
Hi Adnan,
I understand that you want to select columns of matrix TheArray according to the output of the logical array and find out the mean of selected range of parameter value. You also want to store columns from the matrix TheArray into another variable. I assumed that the window for comparison is 0.5. You can achieve the desired task using for loop as follows:
lowerLimit = 1.0; % minimum value of the parameter
UpperLimit = 10; % maximum value of the parameter
stepsSize = 0.5; % window length
rangeVec = lowerLimit:stepsSize:UpperLimit;
TheArray=A; % Data set after preliminary processing
pds = zeros(length(rangeVec)-1,1);
for iterVar = 1 : length(rangeVec)-1
C = rangeVec(iterVar); % lower limit for parameter value
D = rangeVec(iterVar + 1) ; % upper limit of the parametere values
mask = (TheArray(1,:) < D & TheArray(1,:)>C); % Masking condition
output = TheArray(:,mask); % selected array
pds(iterVar) = mean(output(1,:)); % mean of the parameteres velues of the selected coloumns
ev = output(2:end,:); % Delete parametere row from selected data
strVar = strcat('d',num2str(iterVar),'= ev(:)'); % creates string for input to the evalin function
evalin('base',strVar); % matrix converted to column matrix
end
For more info, refer to the documentation of strcat, num2str, and evalin functions

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by