Asked by Zoe
on 7 Jul 2017

I have the following code that loops over each pixel of a .tif image to predict responses using ensemble of regression models.

X is a 753*6 numeric array which has 6 variables (also columns), and 753 rows. NR = 1380, NC = 1464.

I understand the error's meaning (The dimensions on both sides do not match each other), but I really do not know how to fix it. I imagine the result I need should be a 1380*1464 numeric array.

a = imread('LE71250521999276_b1.tif')

[NR,NC] = size(a);

Yfit = zeros(NR,NC);

for i = 1:NR

for j = 1:NC

Yfit(i,j) = predict(Mdl1999276,X);

end

end

ERROR: Assignment has more non-singleton rhs dimensions than non-singleton subscripts

Thank you for helping!!

Answer by Walter Roberson
on 7 Jul 2017

Accepted Answer

Why are you using all of X to do the prediction each time?

Why are you reading in the image if you are not going to predict based on its values?

Ensembles often make one prediction per ensemble member per sample; if so then you might need to analyze a vector of results to decide what one output you want.

Predictions sometimes output a probability per class rather than a single class number.

Zoe
on 7 Jul 2017

Thank both of you for answering my question! (a is a grey-scale tiff image, so I guess my old code would be fine?)

The training sample i used to train my ensemble regression model has 6 predictor variables, so that the testing sample (X in the code) should also be a 6-column matrix or table. These 6 variables are DN values of different bands. Therefore I think the predictors and the image itself are related somehow.

I also tried this following code (changed a bit), the for loop DID work, but it returned me a 753*1 numeric array (same # of rows to the X input), but not a NR*NC array of predicted values that I expected. So the problem is how can I get a NR*NC array of predicted Y value then? Thank you again!

for i = 1:NR

for j = 1:NC

Yfit = predict(Mdl1999276,X); % I deleted (i,j)

end

end

Walter Roberson
on 7 Jul 2017

Given any one pixel, do you need the full 753*6 X array to predict for it?

I suspect that you already built the knowledge in X into Mdl1999276 and should not be passing it in. I suspect you should be using

for j = 1 : NC

Yfit(:,j) = predict(Mdl1999276, a(:,j));

end

or something similar.

Noor Abbas
on 19 Mar 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## donald adams (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/347675-how-to-predict-each-pixel-of-image-using-regression-model#comment_507122

Sign in to comment.