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

조회 수: 6 (최근 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