Extracting minimum and maximum values from dimensions of a matrix

1 Ansicht (letzte 30 Tage)
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?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Apr. 2018
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 Kommentare
Walter Roberson
Walter Roberson am 15 Apr. 2018
Bearbeitet: Walter Roberson am 15 Apr. 2018
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by