I think it's a simple one
I have 8 arrays T03 and I want to check for some conditions in every one of them in a single loop. Here's what I'm talking about:
while(i<length(RRs)+1)
if T03_i(2,4)>0 && RRs(1:2,i)<0
RRs(1:2,i) = 0;
end
i=i+1;
end
As you see, I want to change elements in RRs array based on conditions in both RRs and T03_1/T03_2/T03_3/.../T03_8. Since T03_i doesn't work, do you have any suggestions?
Seems like the T03_i arrays are of the same size. You can combine them in one 3D array like this:
T3D = cat(3,T03_1,T03_2,T03_3,T03_4,T03_5,T03_6,T03_7,T03_8)
Then
V_mn = squeeze(T3D(m,n,:));
will give you a vector of elements from m-th row and n-th column from all the T03_i arrays at once and you can loop through them like V_mn(i) (here i has the same meaning as in your T03_i).
I suppose that you need all of the T03 arrays to be checked:
while(i<length(RRs)+1)
if T03_1(2,4)>0 ...
&& T03_2(2,4)>0 ...
&& T03_3(2,4)>0 ...
&& T03_4(2,4)>0 ...
&& T03_5(2,4)>0 ...
&& T03_6(2,4)>0 ...
&& T03_7(2,4)>0 ...
&& T03_8(2,4)>0 ...
&& RRs(1:2,i)<0
RRs(1:2,i) = 0;
end
i=i+1;
end
Related
I have an array like t. It contains the numbers and I would like to add to each number the previous ones. For example: t=[0,2,3,5] and I would like to get tnew=[0,2,5,10]. I tried out this code but it is wrong for sure. (There are 5292 values)
for i=0:5292
t(i)=t(i)+t(i+1)
end
For some array t = [0,2,3,5];, you can just do tnew = cumsum(t).
If you really want to do this in a loop, you need to start from the 2nd index, and keep adding to the value from the previous index
t = [0,2,3,5];
tnew = t;
for ii = 2:numel(t)
tnew(ii) = t(ii) + tnew(ii-1);
end
I have been trying to execute a piece of code with some if conditions. This is a simple version of it.
X=100;Y=100;
if ((((X+1) && (Y+1))<=99) && (((X+1) && (Y+1))<=102))
disp(X);
end
Despite both X and Y not satisfying the first condition, I still get the output as 100. I have tried all combinations of & and && to make the and operations in the work. I checked the difference between the two and I found that & is a logical bit-wise operator while && is a short-circuit operator, which probably doesn't change much in this context. What's the error with this syntax?
Of course the code works when I do this:
X=100;Y=100;
if (X+1)<=99 && (Y+1)<=99 && (((X+1) && (Y+1))<=102)
disp(X);
end
But this is so inefficient when there are lot of conditions and each sub-condition must include the constraints. I'm sure this must be answered somewhere and this question might be a duplicate, so please point me to the answer.
So it looks like you understand what (X+1)<=99 && (Y+1)<=99 does. Let's look at ((X+1) && (Y+1))<=99:
&& requires a logical value on each side. a && b will turn a and b into logicals, effectively becoming a~=0 && b~=0. Thus:
((X+1) && (Y+1) ) <= 99
((X+1)~=0 && (Y+1)~=0) <= 99
( true && true ) <= 99
1 <= 99
true
Of course the truth value of (X+1)~=0 and (Y+1)~=0 could be different, but here you see this. In MATLAB, true is equal to 1 in a non-logical context, as when compared to 99.
If you want to simplify this expression, use max instead of &&:
X=100;Y=100;
if max(X+1,Y+1)<=99 && max(X+1,Y+1)<=102
disp(X);
end
If the max of a and b is smaller than 99, then both a and b are smaller than 99.
(Obviously, the statement can be further simplified to if max(X+1,Y+1)<=102, since if the second inequality holds than so must the first.)
How can I improve (shorten) my check that values are real positive integer? I want to do it first for just an integer (a), for which I have tried
a~=floor(a) || a<=0 || imag(a)~=0
Secondly, I'd like to do it for a linked list
l=struct('xpos',value,'ypos',value,'entry',value,'next',[])
l=struct('xpos',value,'ypos',value,'entry',value,'next',l)
and so on.
For the list I want all xpos and ypos values to be real positive integers, I have tried
any(l.xpos~=floor(a) || l.xpos<=0 || imag(l.xpos)~=0 || l.ypos~=floor(l.ypos) || l.ypos<=0 || imag(l.ypos)~=0)
This ends up being quite long when I had two lists to check.
You could possibly simplify your check and turn it into an anonymous function using something like:
isPositiveInt = #(x)isscalar(x) && isnumeric(x) && abs(round(x)) == x && x > 0;
If it's guaranteed that it will always be a scalar and numeric you can simplify this to:
isPositiveInt = #(x)abs(round(x)) == x && x > 0;
Then you could easily perform this check for both xpos and ypos
isPositiveInt(S.xpos) && isPositiveInt(S.ypos)
To go through your "linked list" you'll just need to loop through it
while ~isempty(S)
% They they are both integers do something
if isPositiveInt(S.xpos) && isPositiveInt(S.ypos)
% Do something
end
% Go onto the next one
S = S.next;
end
As a side-note, it's best to avoid using a lower-case l as a variable since it looks a lot like the number 1. I have switched it out for S in the example above.
I have written a function which takes in an integer (int8) as one of the inputs (called iscool). The function runs a while loop and I insert an if-check inside it to break out of the loop. The if-check checks the value of the iscool variable as well and sets the value of imarch to 0 to get out of loop. So basically, the code is something like this.
% Code_snippet
while (imarch == 1)
<some procedures not modifying iscool>
if ((iscool == 0) && (<other condition 1>) && (<other condition 2>))
imarch = 0;
elseif ((iscool == 1) && (<other condition 3>) && (<other condition 4>))
imarch = 0;
end
disp (strcat('Cooling index is: ',num2str(iscool)));
end
The output of the disp command in the first while-loop execution is 0 (which is the input), but it changes to 1 in the subsequent iteration and stays so after that. I have tried removing the if-elseif-end check and the value of iscool stays intact in that case, but I need to have the check in order to be able to get out of the loop. Any sort of help, particularly an insight into why the value might be changing would be great help. Thanks.
Here's my code:
N = 1:999;
for i = N
if rem(i,3) == 0 || rem(i,5) == 0
v(i,1) = i
end
end
Te problem is that I get an Array with some zeros in, but I just want an an arraywith the values comforming to my conditions.
How can I fix it?
Thank you!
I think the OP is looking for a result like:
v= N( (rem(N,3)==0) | (rem(N,5)==0) );
though without looping... :-)
I'm assuming that you're using a loop for a reason, and am not removing it from my solution. However, loops should be avoided where possible.
If I understand your question, you're trying to store only those values of i which correspond to a true conditional evaluation. You're problem is that you're using i as your index value inside the assignment statement. Use the end index keyword. Like so:
N = 1:999;
v = [];
for i = N
if rem(i,3) == 0 || rem(i,5) == 0
v(end+1) = i
end
end