how to compare 2 double array

25 次查看(过去 30 天)
Suppose i have 4 double array a(i,j) of size 256*8 elements and y(i1,j1),z(i2,j2) and b(i3,j3)having size of 1*8,1*8,1*50 elements.
I want to compare all the rows of a(i,j) with y(i1,j1),z(i2,j2) and b(i3,j3). The condition is
if (a(i,j)==y(i1,j1) or a(i,j)==z(i2,j2)) and b(i)==0
a(i,j)=y(i1,j1);
elseif b(i)==1
a(i,j)=z(i2,j2);
can anybody write the code perfectly.
  2 个评论
Jan
Jan 2016-11-15
It is unlikely that forum users will write code for you. We all have out own projects to work on. But if you show, what you have tried so far, suggestions for improvements are likely.
Guillaume
Guillaume 2016-11-15
编辑:Guillaume 2016-11-15
In addition to what Jan and KSSV said, the test does not appear to make much sense. As written, it is
  • if b(i) is 1, (whatever i refers to) then the values of a, y and z don't matter, just assign something from z to something from a.
  • if b(i) is 0, then if something from a matches something from y or z then assign something for y to something from a
  • if b(i) is 0, but the something from a matches neither y nor z, then nothing happens
That's not very symmetrical so probably not what is intended.
Note all the something in the above. That's because the notation used is very confusing and I've no idea what all the indices are supposed to be. Are these indices totally independent, resulting in a comparison of all the elements of a (256*8 elements) with all combinations of the elements of y , z, and b (8*8*50), for a total of (256*8)*(8*8*50) comparisons or should only column 1 of a be compared to column 1 of y and z (but to all columns of b?)? Perhaps, only rows of a should be compared to columns of b (since they both use the same index i) but it is stated that a has 256 rows while b only has 50 columns...

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-11-15
编辑:Image Analyst 2016-11-15
I see nothing inherently wrong with your code other than using "and" and "or" for && and . And not having an "end" on your if/else block of course. Fix those problems and I think it should work with numbers that are integers or powers of 2. Of course your indexes i, j, i1, j1, i2, j2, i3, and j3 all need to be defined of course, perhaps in a for loop or whatever.
If you have fractions (non-integers and not inverse powers of 2), then see the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

更多回答(1 个)

KSSV
KSSV 2016-11-15
Why someone has to write code for you?
doc find, ismember, ismemebrtol.
As the numbers are flottant, find, == may not work. In that case ismemebrtol should work. Or get the difference of the numbers you are comparing and see whether the the difference is less then the tolerance limit, which you have to provide.

Community Treasure Hunt

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

Start Hunting!

Translated by