Why do I receive error for reshape function?

I am using the following code below, however, I keep getting geting the error message for reshape function. How would the code or reshape function be changed to stop the error?
>> s = 1/sqrt(2);
A = [1 1 1 0 0 0 0 0 0; 0 0 0 0 0 0 1 1 1; 1 0 0 1 0 0 1 0 0; 0 0 1 0 0 1 0 0 1; s s 0 s 0 0 0 0 0; 0 0 0 0 0 s 0 s s; 0 0 0 s 0 0 s s 0; 0 s s 0 0 s 0 0 0];
b = [6; 24; 12; 18; 4.9497; 16.2634; 13.4350; 7.7781];
k = 1:20;
X = kaczmarz(A,b,k);
figure(1)
for i=1:kmax, subplot(4,5,i), imagesc(reshape(X(:,i),N,N)), end
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.

3 件のコメント

Chunru
Chunru 2021 年 6 月 7 日
Check the size of X before figure(1):
size(X)
Check if the first dimension of X is equal to N*N
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 7 日
I suspect the issue is that X(:,i) does not have exactly NxN elements.
Ryan
Ryan 2021 年 6 月 7 日
I've checked the size of X:
size(X)
ans =
9 7
>> N*N
ans =
4096
What would I need to change X size to be to get figure(1)?

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

 採用された回答

KSSV
KSSV 2021 年 6 月 7 日

0 投票

The number of elements in X(:,j) is not equal to N*N, thats why you are getting error.
N = 3 ;
X = rand(10) ;
x = reshape(X(:,1),N,N) ; % this will lead to 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.
See to it that the number of elements should not change. You may use imresize to achieve this.
[m,n] = size(X) ;
X = imresize(X,[N*N,n]) ;
imagesc(reshape(X(:,1),N,N)) ; % this will work

3 件のコメント

Ryan
Ryan 2021 年 6 月 7 日
In my next line of code I've used
k = [50,100,200,400,800];
X = kaczmarz(A,b,k);
e = zeros(length(k),1)
for i=1:length(k), e(i) = norm(x-X(:,i),inf); end
e
figure(2), semilogy(k,e)
This has created a matrix dimentions must agree error?
KSSV
KSSV 2021 年 6 月 7 日
It would be because the dimensions (number of rows) in x and X are different. You can change the dimensions of x or use same X as previous.
Ryan
Ryan 2021 年 6 月 7 日
Great! All working now, thanks for the help!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 6 月 7 日

コメント済み:

2021 年 6 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by