How do I multiply 2 1 by 200 vectors and record when a particular answer is achieved?
Show older comments
I need to do some mixer calculations. I have 2 1x200 double vectors. I need to multiply each member of 1 vector by a constant, say 3, and then subtract 2 times the appropriate member of the second vector. In other words, I need to get the solution to 3*a(1)-2*b(1) and get an answer. If the answer is 45, I need to note what a(x) and b(x) produced that result. I need to go through all 200 members of each vector. So, 40,000 calculations. In other words, starting as mentioned, the next problem to be solved would be 3*a(1)-2*b(2). If the answer is 45, record a(1) and b(2). If the answer is not 45, then solve 3*a(1)-2*b(3), etc until 3*a(1)-2*b(200). Then, we would go to 3*a(2)-2*b(1) until we get to 3*a(2)-2*b(200), etc. What is the easiest way to do that in Matlab. I have the latest version?
Accepted Answer
More Answers (1)
Geoff Hayes
on 19 Jun 2015
Ed - the easiest (and so not the most efficient) is to use two for loops to iterate over the data. For example,
A = randi(1000,1,200);
B = randi(1000,1,200);
for u=1:200
for v=1:200
if 3*A(u)-2*B(v) == 45
fprintf('%d and %d produce 45 (A(%d),B(%d))\n',A(u),B(v),u,v);
end
end
end
The above code will write out the pair that (according to your equation) produces 45.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!