Extracting minimum and maximum values from dimensions of a matrix
1 回表示 (過去 30 日間)
古いコメントを表示
Faisal Alnahhas
2018 年 4 月 12 日
コメント済み: Faisal Alnahhas
2018 年 4 月 15 日
I have dataset that looks like below (this is just a sample), the last column (with 1, 2,..) are classes. I need to extract the min and max for each column for each class. That means for all columns that have i at end, I need the min and max of all columns. Example:
Data:
0.5000 0.4600 0.6400 0.3600 0.5000 0 0.4900 0.2200 1
0.5300 0.5600 0.4900 0.4600 0.5000 0 0.5200 0.2200 1
0.5200 0.5300 0.5800 0.6900 0.5000 0 0.5000 0.2200 3
0.3900 0.5300 0.5800 0.6900 0.5000 0 0.5000 0.2200 2
0.5000 0.4600 0.6400 0.3600 0.5000 0 0.4900 0.2200 2
Output:
Class 1, Dim 1, Min 0.5000, Max 0.5300
Class 1, Dim 2, Min 0.4600, Max 0.5600.
Now I used to calculate mean of such data using the following code (works perfectly):
for i = 1:10
Mean(i,: ) = mean(file(file(:,end)==i,:));
end
When I try to apply same logic to min, using:
s(i,:) = min(file(file(:,end)==i,:)) (in the same loop)
It works for the first class of data (1), then I get the following error: Subscripted assignment dimension mismatch. How can I fix this, or what is the best way to do what I want to do?
0 件のコメント
採用された回答
Walter Roberson
2018 年 4 月 12 日
When you get to i = 4, then file(:,end)==i has no true values and file() indexed there is empty. min() of empty is empty. You cannot assign an empty value to a definite location s(i,:)
9 件のコメント
Walter Roberson
2018 年 4 月 15 日
編集済み: Walter Roberson
2018 年 4 月 15 日
Change
rows = size(file_train, 1);
to
unique_cases = unique(file_train(:,end));
rows = length(unique_cases);
and change
for i = 1:rows
%min(THEARRAY, [], 1)
s(i,:)= min(file_train(file_train(:, end) ==i,:), [], 1);
l(i,:) = max(file_train(file_train(:, end) ==i,:), [], 1);
end
to
for row_idx = 1:rows
%min(THEARRAY, [], 1)
i = unique_cases(row_idx);
s(row_idx,:)= min(file_train(file_train(:, end) ==i,:), [], 1);
l(row_idx,:) = max(file_train(file_train(:, end) ==i,:), [], 1);
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!