using logic operators
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have 2 column vectors x2=[3;4;5] and x3=[2;5;4] and a 4X5matrix of ones and zeros say x5=[1 1 0 1 0;1 1 1 1 0;1 1 0 1 0;1 1 1 1 0]. I want to generate a 3X2 matrix of 2s and 1s using the following logic operations and making element wise comparison:
function result=myfunct.m
for j=1:5
ii=1:3;
if ( x5(j,5)==1 & x2(ii)<4)
q1=2;
else
q1=1;
end
if ( x5(j,5)==1 & x3(ii)<3)
q2=2;
else
q2=1;
end
result1=q1;result1(ii)=result1;result2=q2;result2(ii)=result2;
result=[result1,result2];
end
The problem I'm having is that the program instead of comparing each element of x5(j,1) to 1 and each element of x2 (or x3) to 4 and returning the result, it is actually comparing the column vector of x5(j,1) j=1:5, to 1 and the column vector of x2(ii) ii=1:3 and returns me the result q1=2 if true and q1=1 if false. Can anybody help out on how to make this one to one comparison? Thanks in advance!
0 Commenti
Risposte (1)
Geoff
il 3 Feb 2012
There's a few odd things going on here...
First, the valid range for j as used here is 1:4, not 1:5 for a 4x5 matrix.
As for the ii variable, did you mean to make a nested for-loop? By comparing x2(1:3) < 3, you will get a vector result hence why your code won't work with the more usual && operator. Perhaps you meant this:
any(x2(ii) < 4)
As for the result, it is written in a very confusing way that doesn't look at all like what you may have intended. Maybe if you explained what this code is trying to achieve in plain english, it would be easier to work out what you're doing wrong. =)
-g-
0 Commenti
Vedere anche
Categorie
Scopri di più su Get Started with Optimization Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!