Hi all,
so I have the follwoing problem. I have data stored in a (2000,4) mattrix where the last colum represents the predictions I drew from a logistic regression. Now, I managed to put in a messhgrid data on columns 2 and 3 (first term is the constant) as height and weeightt as follows:
h = min(X(:,1)):.01:max(X(:,1)); %(1,2069)
w = min(X(:,2)):1:max(X(:,2)); %(1,24)
[hx, wx] = meshgrid(h,w); %(24,2069)
but did not manage to put in the same form the predictions since I computed in a later step. Predictions are a (2000,1) vector.
The aim basically is to make this:
pred = reshape(pred, size(hx));
work without the error:
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size
for that dimension.
which, if I have undestood the issue is solved if I manage to put pred in a vector of size "size(hx, 1) * size(hx, 2)".
Can you pleasse help m out on this?

 採用された回答

KSSV
KSSV 2021 年 5 月 5 日

1 投票

Let X be your data matrix of size 2000*4.
h = X(:,2) ;
w = X(:,3) ;
nh = length(unique(h)) ;
nw = length(unique(w)) ;
h = reshape(h,nh,nw) ;
w = reshape(w,nh,nw) ;
p = reshape(X(:,4),nh,nw) ;
If the above works, it is good. If not have a look on griddata function. If not attach your data.

6 件のコメント

federico nutarelli
federico nutarelli 2021 年 5 月 5 日
@KSSV thank you for your reply. AActually h and w are not the econd and third columns of X but are derived from X as follows (dimensions are commented):
h = min(X(:,1)):.01:max(X(:,1)); %(1,2069)
w = min(X(:,2)):1:max(X(:,2)); %(1,24)
[hx, wx] = meshgrid(h,w); %(24,2069)
I am actually trying o reproduce Lab:SVM of this website. In particular I have done the logistic function and obtained the prediction called "pred", but cannot reshape them properly
KSSV
KSSV 2021 年 5 月 5 日
h = min(X(:,1)):.01:max(X(:,1)); %(1,2069)
w = min(X(:,2)):1:max(X(:,2)); %(1,24)
[hx, wx] = meshgrid(h,w); %(24,2069)
pred = griddata(X(:,1),X(:,2),X(:,4),h,w) ;
federico nutarelli
federico nutarelli 2021 年 5 月 5 日
編集済み: federico nutarelli 2021 年 5 月 5 日
It raiss the error:
Error using griddata (line 106)
XI and YI must be the same size or vectors of different orientations.
Actually: X(:,1),X(:,2),X(:,4) have all the same dimension (2000,1). But h and w have different dimensions (as above)
federico nutarelli
federico nutarelli 2021 年 5 月 5 日
@KSSV I attached the zip of th entir foldr. I am using main.m. From line 44 to line 73 I am applying logistic regression. he problem arises at lines 108-109, where I should reshape data to construct a countourf()
KSSV
KSSV 2021 年 5 月 5 日
You need to use:
pred = griddata(X(:,1),X(:,2),pred,hx,wx) ;
federico nutarelli
federico nutarelli 2021 年 5 月 5 日
It works!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDescriptive Statistics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by