Unable to perform assignment because the indices on the left side are not compatible with the size of the right side

4 visualizzazioni (ultimi 30 giorni)
Hello, I am getting this error whenever I try to run this.
Problem: NN1 is my data set, column 1-5 are inputs and 7th is the target. Funny thing is that it works perfectly well when there were half of the data, but even if i add 1 more row of data in the input and target column, this error is displayed.
y and y2 infos:
y is 132x1 double
y2 is 132x1 complex double
Thanks in advance
data = readmatrix('NN1.xlsx');
x = data(:,1:5);
y = data(:,7);
m = length(y);
histogram(x(:,2),50);
plot(x(:,1),y,'o')
y2 = log(1+y);
for i = 1:5
x2(:,i) = (x(:,i)-min(x(:,i)))/(max(x(:,i))-min(x(:,i))); %Error here
end
histogram(x2(:,5),50);
plot(x2(:,5),y2,'o');

Risposta accettata

Jon
Jon il 3 Dic 2021
Modificato: Jon il 3 Dic 2021
Following up on @Walter Roberson's comment, I ran your code (I assigned my own values to data, as I didn't have your input file) and found that it can run repeatedly as long as you don't change the number of rows in data. If you run the code first say with data having 132 rows, and then run it again, without clearing the workspace with data having 133 rows you get that error.
This makes sense as the first time through the loop on the second run x2 is already a 132 x 5 matrix. But you are trying to assign a 133 x 1 vector to its first column. This won't work.
You could simply clear the workspace, or just x2 between runs, but the real problem here is that you are not preallocating your arrays. If you do this as shown below it will use MATLAB's memory more efficiently, and also avoid these type of problems with assigning into arrays that already have values from some previous run. Please also seehttps://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
data = rand(132,7)
x = data(:,1:5);
y = data(:,7);
m = length(y);
histogram(x(:,2),50);
plot(x(:,1),y,'o')
y2 = log(1+y);
% preallocate array to hold results
numRows = size(data,1);
x2 = zeros(numRows,5);
for i = 1:5
x2(:,i) = (x(:,i)-min(x(:,i)))/(max(x(:,i))-min(x(:,i))); %Error here
end
histogram(x2(:,5),50);
plot(x2(:,5),y2,'o');
Finally, in your particular example, you don't need to use a loop or preallocate at all. You can do everything in a "vectorized" fashion using just the one line of code:
x2 = (x - min(x))./(max(x) - min(x)) % note the ./ to give element by element division
  5 Commenti
Jon
Jon il 6 Dic 2021
Oh, I missed that it actually did the operation the OP was asking for. That's really nice! Thanks that's a nice useful function to know about.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by