How do I compare of two arrays in matlab? [duplicate] - arrays

This question already has answers here:
Comparing two matrices in Matlab
(6 answers)
Closed 8 years ago.
i've got an array like x, i want to do some works on it and put result in the new array y. then i should compare this two. if they are the same by a thershold(i.e they could be a little different) that's ok and algorithm ends otherwise i should continue the iteration
the problem is comparing these two.
they are a two 2d array with unknown elements.
i've done two different way but none of them where ok:
first way:
d = x - y
if d < 5
disp('end')
end
and so on
but it does not work well,honestly it doesn't work at all
the other way which i used is:
isequal(x,y)
while they are the same it will return 0 but if they are not and even with a little difference the result will be 1 and it is not ok cause as i said algorithm should consider a litlle difference and stop the iteration
what should i do?

If 5 is an OK threshold, then this should work:
d=abs(x-y);
if all(d<5)
disp('end')
end
If you don't know what the threshold is, then that's a very different question. Determining a sensible threshold is dependant on your application, and is often a trade-off - there may not be a "right" answer if your data is variable. Look into some basic statistics - the zscore command may be a useful start.

One other way to inspect the difference vector is to use "find()" function in MATLAB. As Nolan, I think you better use the absolute value of the difference.
idx = find(abs(a-b)>threshold) will give you the indices that exceed the threshold. If null, then you terminate your iterations.

Related

How do I generate a string array in MatLab 2016b? [duplicate]

When trying to run my code, for example
for ii= 1:10
output(ii)=rand(3);
end
I get the error
In an assignment A(:) = B, the number of elements in A and B must be the same
or
In an assignment A(I) = B, the number of elements in B and I must be the same.
What does this error mean? What is the approach to get rid of it?
This error comes because you are trying to fill a variable chunk with more (or less) values than its size. In other words, you have a statement A(:)=B on where size(A(:)) is different to size(B).
In the example in the question, rand(3) returns a 3x3 matrix, however, output(ii) is just a single value (even if output may be bigger, output(ii) is just a single value of output), thus the value returned by rand(3) does not fit inside output.
In order to solve this problem, you need to change the the size of the output variable, so you have space to fit all the result.
There are 2 ways of doing this. One of them is by creating a Matrix that fits the return, e.g. output=zeros(3,3,10).
Then we can change the code to
for ii= 1:10
output(:,:,ii)=rand(3);
end
Alternatively, you can fill the output as a cell array. This is particularly useful when the return of the function changes sizes each time, e.g. rand(ii);
In that case, the following would work
for ii= 1:10
output{ii}=rand(ii);
end
It is probable that unlike in the example in the question, in the real case you do not know the size of what the output returns, thus you do not know which of the two options to use to fix your code.
On possible way of learning that, is activating debugging help when the code errors, by typing dbstop if error in your command line. This will trigger a debugging stop when MATLAB throws an error, and you can type size(rand(ii)) and size(output(ii)) to see the sizes of both.
Often, reading the documentation of the function being used also helps, to see if different sizes are possible.
That said, the second option, cell arrays, will always ensure everything will fit. However matrices are generally a faster and easier to use in MATLAB, thus you should aim for the matrix based solution if you can.

In an assignment A(:) = B, the number of elements in A and B must be the same

When trying to run my code, for example
for ii= 1:10
output(ii)=rand(3);
end
I get the error
In an assignment A(:) = B, the number of elements in A and B must be the same
or
In an assignment A(I) = B, the number of elements in B and I must be the same.
What does this error mean? What is the approach to get rid of it?
This error comes because you are trying to fill a variable chunk with more (or less) values than its size. In other words, you have a statement A(:)=B on where size(A(:)) is different to size(B).
In the example in the question, rand(3) returns a 3x3 matrix, however, output(ii) is just a single value (even if output may be bigger, output(ii) is just a single value of output), thus the value returned by rand(3) does not fit inside output.
In order to solve this problem, you need to change the the size of the output variable, so you have space to fit all the result.
There are 2 ways of doing this. One of them is by creating a Matrix that fits the return, e.g. output=zeros(3,3,10).
Then we can change the code to
for ii= 1:10
output(:,:,ii)=rand(3);
end
Alternatively, you can fill the output as a cell array. This is particularly useful when the return of the function changes sizes each time, e.g. rand(ii);
In that case, the following would work
for ii= 1:10
output{ii}=rand(ii);
end
It is probable that unlike in the example in the question, in the real case you do not know the size of what the output returns, thus you do not know which of the two options to use to fix your code.
On possible way of learning that, is activating debugging help when the code errors, by typing dbstop if error in your command line. This will trigger a debugging stop when MATLAB throws an error, and you can type size(rand(ii)) and size(output(ii)) to see the sizes of both.
Often, reading the documentation of the function being used also helps, to see if different sizes are possible.
That said, the second option, cell arrays, will always ensure everything will fit. However matrices are generally a faster and easier to use in MATLAB, thus you should aim for the matrix based solution if you can.

Pascal - how sets work? [duplicate]

This question already has answers here:
What is the implementation of sets used in pascal?
(2 answers)
Closed 6 years ago.
I'm going to a high school programming competition tomorrow, and they use Pascal, about which I can't find much information on the internet, or if I do, I can't really understand it (English isn't my native language).
It would be much appreciated if - someone who still remembers, would explain me: what is a set? Or, how would it look like in C programming language? I guess it's something related to arrays, but I'm not sure though.
Thanks for help in advance!
A set is an unordered collection of elements in which each element can occurr only once.
Depending on what the unique identification of an element is, there can be many ways to implement a set, in any language.
For example, the unique identification is a name and it is mapped onto a number from zero to the size of the set in some way, and this number is used as an index into an array where each array element is [a pointer to] the element. Or there is an array of 32 bit ints and each bit tells whether the element exists in the set and the elements themselves are stored by number in an ordered linked list.
So you see, whithout having more information of what is to be stored in the set, there are numerous implementations possible.

What does the variable i in for loops stand for?

Having trouble understanding what this variable is and where it gets defined:
for i in range (0, 5):
print i
Prints out number 0 - 4 like expected, but I don't understand what i means.
It's just a variable that assumes the values of the elements of an iterable object.
i is just a name chosen for the variable that holds the current array index in each loop iteration.
This is not hard coded, you can choose any name you want:
for someOtherName in range (0, 5):
print someOtherName
i is very traditional, probably comes from "index".
If you are iterating over something other than an integer index, or think your program could benefit from a more context-bearing name (such as in nested or very complex loops), you should probably give it a different name.
i is just a variable which takes values in the range. It could have been named anything within the rule defined by programming language-grammar.
This variable i is supposed to take values in the range 0 to 5 in the given code.
So, i will iterate from 0 to 4.
5 is exclusive from i as we're talking about range function which excludes the right-bound(right hand side limit).
Nothing, its just a local variable. You could call it just about anything you wanted and use that name in your loop instead.
Because when you have nested for loops the second one usually uses j as its variable and for loops, in general, can be visualized to be iterating through vectors or matrices (if nested), it could be a reference to linear algebra, where there are I and J to represent the two axes as the basis vectors.
For me, i = iteration. That's how I grab it.
From my Knowledge, It comes from FORTRAN, where variables starting with "i"
through "n" were integers.
This is how our developers started using "i" as a standard loop counter over 60 years ago

algo to find number of common characters in 2 strings [duplicate]

This question already has answers here:
Finding common characters in two strings
(13 answers)
Closed 8 years ago.
I am writing a c program to find the number of common characters in two strings.
Eg: aabbccc aabc Ans:4
aabcA aa Ans:2
(Strings will have upper case ,lower case and numbers)
I have two algorithms in my mind
Assuming length of strings is n,m
1.Sort the arrays and then count O(nlogn+mlogm) complexity
2.scan through two strings and use a count arrays - O(n+m) complexity
Can anyone please suggest further optimization or any other methods to do this?
basically you are asking about a Bag(Multiset) Intersection.
and I guess there won't be any more efficient algo than O(n+m) because you will have to go through each and every element of two bags at least once.
Since, optimization is needed for big input, I think your second method is pretty fine(counting array method). Whatever algorithm you try to find out, you can't find the answer to your problem without looking at the two strings completely. Hence, there shouldn't be any further optimization to this problem as it is already O(m+n). I think for smaller input your first algorithm will work faster as there is a constant of O(26+26+10) associated with your second algorithm. But if you are really interested in a faster code then try to optimize the method of reading and writing the output. You may google for "faster I/O in C++" and read about it.

Resources