Coder Structure element Size Mismatch error during subscripting

Hi
I am facing Size mismatch error while performing array operation with structure .I need help in resolving the issue
Impnse.isOut(:,idx) = memfcn(1:size(Impnse.f(:,idx),1),locs);
Error Message : Size mismatch ([441000 x 1] ~= [:? x 441000])
f = zeros(441000, 2)
isOut = zeros(size(f,1),2);
Input Impnse = struct('f',f ,'isOut' ,isOut);
Impnse.isOut(:,idx) = memfcn(1:size(Impnse.f(:,idx),1),locs);
size(1:size(Impnse.f(:,idx),1))
ans =
1 441000
size(locs)
ans =
1423 1
size(Impnse.isOut(:,idx))
ans =
441000 1
Size mismatch ([441000 x 1] ~= [:? x 441000])

6 件のコメント

Adam Danz
Adam Danz 2020 年 8 月 28 日
What is the size of the output?
x = memfcn(1:size(Impnse.f(:,idx),1),locs)
size(x)
Life is Wonderful
Life is Wonderful 2020 年 8 月 28 日
K>> x = memfcn(1:size(Impnse.f(:,idx),1),locs);
K>> size(x)
ans =
1 441000
Life is Wonderful
Life is Wonderful 2020 年 8 月 28 日
編集済み: Life is Wonderful 2020 年 8 月 28 日
With modified setting , I still observe the error
Modified Input
isOut = false(2,size(f,1));
size(memfcn(1:size(Impnse.f(:,idx),1),locs))
1 441000
K>> size(Impnse.isOut(idx,:))
ans =
1 441000
class(Impnse.isOut)
'logical'
class(Impnse.f)
'double'
x = (memfcn(1:size(Impnse.f(:,idx),1),locs));
size(x)
ans =
1 441000
class(x)
ans =
'logical'
Erorr message:Size mismatch ([1 x 441000] ~= [:? x 441000]).
Adam Danz
Adam Danz 2020 年 8 月 28 日
I'm guessing that you need,
Impnse.isOut(idx,:) = memfcn(1:size(Impnse.f(:,idx),1),locs);
% ^^^^^
Life is Wonderful
Life is Wonderful 2020 年 8 月 28 日
Yes, that's correct but how - is not clear to me
Adam Danz
Adam Danz 2020 年 8 月 28 日
Addressed below in my answer.

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

 採用された回答

Adam Danz
Adam Danz 2020 年 8 月 28 日
編集済み: Adam Danz 2020 年 8 月 28 日

0 投票

Impnse.isOut(idx,:) = memfcn(1:size(Impnse.f(:,idx),1),locs);
% ^^^^^
This is matrix indexing 101 and it's a rudimentary concept to understand.
The size of your output is 1 x 441000 which means it's a row vector with 441000 elements.
The output Impnse.isOut(idx,:) means that you're storing the 441000 values in the row number defined by idx.
For example,
a = zeros(3,6)
a =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
a(2,:) = 1:6
0 0 0 0 0 0
1 2 3 4 5 6
0 0 0 0 0 0
With Impnse.isOut(:,idx), you're storing the values in the column number defined by idx.
If the matrix does not have 441000 rows, the data won't fit.
For example,
a = zeros(3,6)
a =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
a(:,2) = 1:6
Error:
Unable to perform assignment because the size of the left side is 3-by-1
and the size of the right side is 1-by-6.
However, if the matrix does have the right amount of rows, the data can be transfered to the column even if it's a row vector.
a = zeros(6,3)
a =
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
a(:,3) = 1:6
a =
0 0 1
0 0 2
0 0 3
0 0 4
0 0 5
0 0 6
I don't know why your error message differs. Perhaps you're using a different version of matlab or perhaps the error message is customized.

6 件のコメント

Life is Wonderful
Life is Wonderful 2020 年 8 月 31 日
編集済み: Life is Wonderful 2020 年 8 月 31 日
Thanks a lot for the help! I did further analysis and have observation. Since we are dealing with logical structure , I added addtional stuff in a small script
a = zeros(3,6) % row,col
% Test purpose
num_row = 2;
num_col = 2;
len_col = size(a,2);
len_row = size(a,1);
len_size_row = 1:size(a,1);
len_size_col = 1:size(a,2);
% evaluation
% store the coloumn values in the row's wise
% Impnse.isOut(idx,:)
a(num_row,1:len_col) = 1:len_col % data values
a(num_row,1:len_col) == 1:len_col % all logical index
a(num_row,num_col) == 1:len_col % logical index of num_col
a(num_row,num_col) = len_col % data values
a(len_row,num_col) = len_col % data values
%Error case
a(len_row,num_col) = 1:len_col; % data values
% Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-6.
a(len_size_row,num_col) = 1:len_col; % data values
% Unable to perform assignment because the size of the left side is 3-by-1 and the size of the right side is 1-by-6.
a(num_row,num_col) = 1:len_col; % data values
% Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-6.
% store the coloumn values in the row's wise
% Impnse.isOut(:,idx)
a = zeros(3,6) % 3row,6col
a(:,num_col)
a(:,num_col) = 1:3 % values
a(:,num_col) == 1:3 % logical index
a(:,num_col) == 1:6 % logical index
% Erorr -case
a(:,num_col) = 1:6 % logical index
% Unable to perform assignment because the size of the left side is 3-by-1 and the size of the right side is 1-by-6.
% change row's to support num coloumns
a = zeros(6,3) % 3row,6col
a(:,num_col)= 1:6 % values
a(:,num_col)== 1:6 % logical index
If Implementation is like below , then it is not doing the intended job
s_data = memfcn(1:size(Impnse.f(:,idx),1),locs);
Impnse.isOut(:,idx) = 1:size(s_data,2); % use repmat(data,row,col)
I don't know why your error message differs. Perhaps you're using a different version of matlab or perhaps the error message is customized.
My version : '9.7.0.1319299 (R2019b) Update 5'
Note : a = zeros(3,6)
a(:,2) = 1:6
The missing part and my understanding
"This is copy index location i.e. starting 1 to 6 but I am looking for the values from 1 to 6 index in the row for a column number."
1:6 is range for copying the data but data in the range is not copied. ONLY range number are copying instead of range(data)
My problem is resolved if following way coding is done
s_data = memfcn(1:size(Impnse.f(:,idx),1),locs);
Impnse.isOut(:,idx) = s_data(:); % use repmat(data(:),row,col)
Please confirm if my understanding is right ? Thank you
Adam Danz
Adam Danz 2020 年 8 月 31 日
編集済み: Adam Danz 2020 年 9 月 1 日
When I test the block of code you shared using r2019b update 6, it produced an error on this line
a(len_row,num_col) = 1:len_col; % data values
Error:
Unable to perform assignment because the size of the left side
is 1-by-1 and the size of the right side is 1-by-6.
This is because len_row=3 and num_col=2 but 1:len_col=[1,2,3,4,5,6]. So, you're trying to put 6 values into a space of 1-value.
I didn't understand what I'm supposed to confirm. Instead of trying to explain it again, I recommend spending an hour or so reading through this page and practicing indexing. Indexing is an important thing to understand if you're going to use matlab. Once you understand this page, you will be able to solve this.
Life is Wonderful
Life is Wonderful 2020 年 9 月 1 日
編集済み: Life is Wonderful 2020 年 9 月 1 日
Hi Adam,
I went through the this page and found interesting tricks. I found Shifting the Rows of a Matrix very interesting.
For the implementation point of view , below logic is solving the size mismatch error
s_data = memfcn(1:size(Impnse.f(:,idx),1),locs);
Impnse.isOut(:,idx) = s_data(:); % copy rows in the respective column number
Thank you very much for guidenace and help!
Adam Danz
Adam Danz 2020 年 9 月 1 日
Nice work!
Life is Wonderful
Life is Wonderful 2020 年 10 月 8 日
Hi Adam,
Can you please help me with below link issue ?
Thank you!
Life is Wonderful
Life is Wonderful 2020 年 10 月 27 日
編集済み: Life is Wonderful 2020 年 10 月 27 日

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by