Extracting minimum and maximum values from dimensions of a matrix

조회 수: 1 (최근 30일)
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?

채택된 답변

Walter Roberson
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
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by