Need help with looping to populate matrix using if statement

1 回表示 (過去 30 日間)
Kevin Hout
Kevin Hout 2016 年 3 月 7 日
回答済み: Kevin Hout 2016 年 3 月 7 日
clc;
clear all;
close all;
load('Stress_Strain.mat')
count = 0;
for i = 1:1000
if Stress_Strain_2(:,2) == count;
StressStrain(i,2) = Stress_Strain_2(i,2);
end
count = count + 0.001
end
% for i = 1:1000
%
% if Stress_Strain_2(i,2) = count && Stress_Strain_2(i,2) > 0.0999;
% Percent_Strain_2(1,2) = Stress_Strain_2(i,2);
% Percent_Strain_2(1,1) = Stress_Strain_2(i,1);
% elseif Stress_Strain_2(i,2) < 0.2001 && Stress_Strain_2(i,2) > 0.1999;
% Percent_Strain_2(2,2) = Stress_Strain_2(i,2);
% Percent_Strain_2(2,1) = Stress_Strain_2(i,1);
  1 件のコメント
KSSV
KSSV 2016 年 3 月 7 日
編集済み: KSSV 2016 年 3 月 7 日
What exactly you want to do? Explain the subject behind it not the coding thing.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 3 月 7 日
Two floating point numbers constructed different ways often do not exactly equal each other. You should be testing for being "close" rather than being "equal".
If you have R2015a or later you should consider using imembertol()
You also have the problem that
if Stress_Strain_2(:,2) == count
means the same thing as
if all(Stress_Strain_2(:,2) == count)
which is to say that all of the items in the column would have to be the same value, all exactly equal to what is in "count". This is unlikely to be what you wanted. You did not describe your desired match condition, so I cannot repair the code. Possibly you want
if any(Stress_Strain_2(:,2) == count)
except with correction for tolerances. Perhaps
if ismembertol(count, Stress_Strain_2(:,1))

Kevin Hout
Kevin Hout 2016 年 3 月 7 日
Thanks, Walter.
I was trying to take a value in the Stress column (at Strains equal to 0.001, 0.002, 0.003, etc) and populate it into a new table. I am trying to use an if statement, but it is not working.

カテゴリ

Help Center および File ExchangeStress and Strain についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by