필터 지우기
필터 지우기

Error using reshape: Size arguments must be real integers. Except all values are positive whole numbers?

조회 수: 4 (최근 30일)
*** I have attached the variable in question as a .mat file. But I am not sure if anyone will be able to view it. Please comment if not and I will try and share an excel version of the information. ****
I have some data inside a variable. It is dynamically allocated so sometimes the values are different. But I have created a way to make sure that I only create a matrix that can be reshaped. Here is the code I have:
num_steps = raw_data.num_steps;
% Reshape value matrix and time/source vector
mat_size = size(raw_data.variable_mat);
Unable to resolve the name raw_data.variable_mat.
% If the variable matrix has dimensions that will cause
% probems for reshaping, just remove data points to make it
% work.
reshape_val = floor(mat_size(2) / num_steps);
if reshape_val ~= mat_size(2) / num_steps
raw_data.variable_mat = raw_data.variable_mat(:,1:reshape_val);
end
mat_size = size(raw_data.variable_mat); % Recreate mat_size for new variable_mat
raw_data.variable_mat = reshape(raw_data.variable_mat, mat_size(1), mat_size(2) / num_steps, num_steps);
But when I run this code, I receive the following error:
Error using reshape
Size arguments must be real integers.
This does not make sense to me as, in this case, you would technically have:
reshape(raw_data.variable_mat,2,10453,3)
where you should receive a 2x10453x3 matrix given that all values for size arguments are real integers (positive whole numbers).
Can anybody help me see what I am doing incorrectly here?

채택된 답변

Jan
Jan 2021년 12월 23일
Change:
raw_data.variable_mat = raw_data.variable_mat(:,1:reshape_val);
to
raw_data.variable_mat = raw_data.variable_mat(:,1:(reshape_val * num_steps));
In the original version you crop too much and mat_size(2) / num_steps need not be an integer.

추가 답변 (1개)

Voss
Voss 2021년 12월 23일
I think the main problem is that you are removing an incorrect number of elements (if I understand what you are trying to do). Have a look at the code below. I removed some semi-colons to show the variables' values, but other than that I only changed how the truncation of raw_data.variable_mat was done:
load('raw_data');
num_steps = raw_data.num_steps
num_steps = 3
% Reshape value matrix and time/source vector
mat_size = size(raw_data.variable_mat)
mat_size = 1×2
2 31358
% If the variable matrix has dimensions that will cause
% probems for reshaping, just remove data points to make it
% work.
reshape_val = floor(mat_size(2) / num_steps)
reshape_val = 10452
if reshape_val ~= mat_size(2) / num_steps
% raw_data.variable_mat = raw_data.variable_mat(:,1:reshape_val);
raw_data.variable_mat = raw_data.variable_mat(:,1:(reshape_val*num_steps));
end
mat_size = size(raw_data.variable_mat) % Recreate mat_size for new variable_mat
mat_size = 1×2
2 31356
raw_data.variable_mat = reshape(raw_data.variable_mat, mat_size(1), mat_size(2) / num_steps, num_steps);
size(raw_data.variable_mat)
ans = 1×3
2 10452 3
Now raw_data.variable_mat is the right size (maybe), but you need to also be sure that the elements are in the order you want after reshaping.
  댓글 수: 1
Steven Manz
Steven Manz 2021년 12월 23일
Yeah, the issue is that I totally forgot to add that I need to multiply the the value by the number of steps. Probably time to stop for the day as I can tell my brain is fried. Thank you for the help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by