Error using reshape: Size arguments must be real integers. Except all values are positive whole numbers?
5 次查看(过去 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);
% 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?
0 个评论
采纳的回答
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
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
% Reshape value matrix and time/source vector
mat_size = size(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);
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
raw_data.variable_mat = reshape(raw_data.variable_mat, mat_size(1), mat_size(2) / num_steps, num_steps);
size(raw_data.variable_mat)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!