I'm trying to use a while loop to delete the final row in an array every time it loops, however, it's not working

3 次查看(过去 30 天)
Basically, using the file TensileData.xlsx that I have attached to the question, I have to use a loop, work backwards through the entire data set reducing the number of points to find the equation of the elastic region. This can be done by calculating the r^2 value until the r^2 value is larger than 0.9997. However, when I try to create write the code for this I am unable to do it. I'm not completely sure where to begin, however, I have made a start. When I run the code I receive the error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in lab8t5 (line 26)
strain(numRows_strain, :) = [];
The code that my question relates to is part b onwards, however, I have put all the code here in case it is helpful.
clear all; close all; clc
% Part a.
data = importdata('TensileData.xlsx');
strain = data.data(:, 1);
stress = data.data(:, 2);
figure(1)
plot(strain, stress, 'r*')
% Part b.
numRows_strain = length(strain);
numRows_stress = size(stress, 1);
[a0, a1, r2] = linreg(strain, stress);
slope = a1;
constant = a0;
error = r2;
while error <= 0.9997
[a0, a1, r2] = linreg(strain, stress);
numRows_strain = numRows_strain - 1;
numRows_stress = numRows_stress - 1;
strain(numRows_strain, :) = [];
stress(numRows_stress, :) = [];
end

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-9-14
Firstly, use readmatrix or readtable instead of importdata.
Secondly, you can define variables from function calls, instead of reassinging them. And as mentioned before, do not use error as a variable name, because it is a built-in MATLAB function.
%Directly use this
[slope, constant, err] = linreg(strain, stress);
Thirdly, you are not updating the variable error in the while loop, so depending upon its value, the while loop will not run or run forever.
% Part a.
data = readmatrix('TensileData.xlsx');
strain = data(:, 1);
stress = data(:, 2);
figure(1)
plot(strain, stress, 'r*')
% Part b.
[slope, constant, err] = linreg(strain, stress);
while err <= 0.9997
%Removing the last row
strain(end, :) = [];
stress(end, :) = [];
%Computing the r_square value again
[~, ~, err] = linreg(strain, stress);
end
Since I do not have the linreg() function, I can not run the above code, but it should do what you asked for - delete the last row till the condition is met.
  2 个评论
Image Analyst
Image Analyst 2023-9-14
You can make it more robust like this
while err <= 0.9997 && ~isempty(strain) && ~isempty(stress)
% Remove the last row from each matrix:
strain(end, :) = [];
stress(end, :) = [];
% Bail out if either matrix is empty.
if isempty(strain) || isempty(stress)
break;
end
% Compute the r_square value again.
[~, ~, err] = linreg(strain, stress);
end

请先登录,再进行评论。

更多回答(1 个)

Richard Burnside
Richard Burnside 2023-9-14
编辑:Richard Burnside 2023-9-14
I think you could just do something vectorized like this:
idx = find(r2>0.9997);
stress(idx) = [];
strain(idx) = [];
Also I think "error" is a Matlab function so you should avoid using it as a variable.

类别

Help CenterFile Exchange 中查找有关 Stress and Strain 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by