Borrar filtros
Borrar filtros

How to Compare a vector with for loop iterations in a nested if statement in a for loop

5 visualizaciones (últimos 30 días)
I'm trying to write a nested if statement within a for loop, where I compare a user input vector of numbers (1,2,3,4,etc.) to the iterations of a for loop (e.g. for i=1:length(a vector)). I need to compare each iteration of the for loop with the user input vector of values, and if they match, the code will return a certain equation; if not, the code will return a different equation. Ultimately, I need a column vector of equations based on the if/for loop. Any thoughts?
function [ DeltaO2Concentration ] = O2Solver_beta(t,O2Conc,c,O2Leak_term,Num_nodes)
%UNTITLED Test function used to solve 'first attempt' code.
% System of ODE diffusion equations that will be solved using 'ode45'
% function. Inputs are constant composed of universal and calculated
% constants, time (independent variable), O2Conc (dependent variable),
% O2Leak which is a separate term or something.
%VECTOR LENGTH WILL NEED TO VARY ACCORDING TO USER INPUT%
start= 0;
terminate= Num_nodes;
DeltaO2Concentration= zeros(1,terminate-start); %Initialize Vector
cnt= 1;
for i= 3:length(DeltaO2Concentration)
if %THE ITERATION MATCHES A VALUE IN THE USER INPUT VECTOR
%RETURN EQUATION 1
ifelse
%RETURN EQUATION 2
end
cnt= cnt+1;
end
DeltaO2Concentration= DeltaO2Concentration';
end
Here is the main script, for reference
prompt_node= {'Enter Selected Node Numbers (e.g. 1,2,3,4,...)'};
dlog_title= 'User Input';
answer_nodes= inputdlg(prompt_node, dlog_title);
Slt_nodes= strsplit(answer_nodes{1},',');

Respuesta aceptada

dpb
dpb el 16 de Jun. de 2017
...
for i= 3:length(DeltaO2Concentration)
if ismember(i,USER_INPUT_VECTOR) % USER_INPUT_VECTOR is your list variable, didn't see it???
%RETURN EQUATION 1
else
%RETURN EQUATION 2
end
...
Alternate way to write the test would be
if any(i==USER_INPUT_VECTOR) % match
...
The first always returns a scalar logical if the index value is first in the argument list whereas the latter returns a logical variable of length(USER_INPUT_VECTOR) with T for the position that matches, if there is a match.
I don't recognize which argument is supposed to be this list from the code; the help text doesn't explain the input variables which isn't of much help to anybody besides the author... :) It, could, I suppose be the last "Num_nodes" I guess, but you didn't show the call to the routine in the higher-level code to do an argument association matching....anyways, use the right variable where I left the placeholder.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by