Error with Min-Max Scaling

20 ビュー (過去 30 日間)
Kyle Koutonen
Kyle Koutonen 2021 年 4 月 25 日
コメント済み: Steven Lord 2021 年 4 月 25 日
The code below fails due to line 5, Just trying to make simple min max scaling code in range of -1 and 1
t=[ 1 5 6; 8 9 7; 2 4 5];
for i= 1:length(t)
Scale1=(t(:,i)-min(t(:,i)))/(max(t(:,i))-min(t(:,i)));
Scalef(i)=2*Scale1 -1
end
Scaled_data=Scalef

採用された回答

Tayyab Khalil
Tayyab Khalil 2021 年 4 月 25 日
First thing, kindly post your code inside a code block to make it easier to read.
I got your code working by simply assigning the scaled value to a column of scalef, the error was that you were trying to assign it as if it were a single value but it is not.
t=[ 1 5 6; 8 9 7; 2 4 5];
for i= 1:length(t)
Scale1=(t(:,i)-min(t(:,i)))/(max(t(:,i))-min(t(:,i)));
Scalef(:,i)=2*Scale1 -1
end
Scaled_data=Scalef
Which gives the following output:
Scaled_data =
-1.0000 -0.6000 0
1.0000 1.0000 1.0000
-0.7143 -1.0000 -1.0000
I don't know if this is what you're looking for, seems to be scaling each column between -1 and 1.
  1 件のコメント
Kyle Koutonen
Kyle Koutonen 2021 年 4 月 25 日
Your code did do exactly what i wanted, and yes I want to scale each row of data. The only porblem is when i implement it into matlab app.designer it no longer works. The code doesn't change, just t changes to app.t since that where the saved data is yet the Scale1 line brings up this error "Index in position 2 exceeds array bounds (must not exceed 2)" what would I need to change
for i= 1:length(app.t )
Scale1=(app.t(:,i)-min(app.t(:,i)))/(max(app.t(:,i))-min(app.t(:,i )));
Scalef(:,i)=2*Scale1 -1
%t2(:,i) = normalize(t(:,i), 'range', [-1 1 ]) ;
end
Scaled_data=Scalef

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 4 月 25 日
t = [ 1 5 6; 8 9 7; 2 4 5]
t = 3×3
1 5 6 8 9 7 2 4 5
t2 = normalize(t, 'range', [-1 1])
t2 = 3×3
-1.0000 -0.6000 0 1.0000 1.0000 1.0000 -0.7143 -1.0000 -1.0000
You can also specify a dimension input.
t3 = normalize(t, 2, 'range', [-1 1])
t3 = 3×3
-1.0000 0.6000 1.0000 0 1.0000 -1.0000 -1.0000 0.3333 1.0000
  2 件のコメント
Kyle Koutonen
Kyle Koutonen 2021 年 4 月 25 日
Your code did do exactly what i wanted, and yes I want to scale each row of data. The only porblem is when i implement it into matlab app.designer it no longer works. The code doesn't change, just t changes to app.t since that where the saved data is yet the t2 line brings up this error "Index in position 2 exceeds array bounds (must not exceed 2)" what would I need to change
for i= 1:length(app.t )
t2(:,i) = normalize(app.t(:,i), 'range', [-1 1 ]) ;
end
Scaled_data=t2
app.UITable5.ColumnName= app.s;
app.UITable5.Data=Scaled_data;
Steven Lord
Steven Lord 2021 年 4 月 25 日
Don't use length on a non-vector especially when you want to iterate over the columns of an array.
M = zeros(5, 4);
L = length(M)
L = 5
In this case L is the number of rows in M rather than the number of columns.
numRows = size(M, 1)
numRows = 5
numCols = size(M, 2)
numCols = 4
% If using a sufficiently recent MATLAB
numRows2 = height(M)
numRows2 = 5
numCols2 = width(M)
numCols2 = 4
In this other case, where M2 has more columns than rows, L2 is the number of columns in M2. But size, height, and width will always return the size in a specific dimension regardless of whether the array is a vector, a tall matrix, a wide matrix, or an N-dimensional array.
M2 = zeros(3, 17);
L2 = length(M2)
L2 = 17

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by