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, :) = [];![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1482611/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1482611/image.png)
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
0 件のコメント
採用された回答
Dyuman Joshi
2023 年 9 月 14 日
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
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
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Stress and Strain についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!