How to make a for loop to go through two arrays in MATLAB - matlab

I want to make a for loop that goes from 0 to 180, and then back again to -180. I tried the following:
for a=0:1:180 && 179:-1:-180
but this is not possible in MATLAB.
I have tried to use the && and || statements, but both don't work. I don't know any other ways to combine the two arrays. Any ideas?

You misunderstand the && and || operators. What you want is the following:
Go from 0 to 180 in steps of 1 AND then go from 180 to -180 in steps of -1.
However for any two statements A and B (both A and B need to be scalar values!), the command A && B does the following:
Return True, if both A and B are True, return False otherwise.
This is a logical AND, while you want to go through your first array AND through your second array after that. Though both is some kind of AND, you can't use && for your purpose.
Now, when you call for a=0:180, MATLAB does the following:
Create the vector 0:180, that is [0, 1, 2, ..., 180].
Run all the content inside the loop for each element in the vector created in 1).
So, what you want to do is create an array that contains the numbers [0, 1, 2, ..., 179, 180, 179, 178, ..., -179, -180]. You can do that by concatenating the arrays [0:180] and [179:-1:-180]. You should read about concatenation in MATLAB in their documentation. So, long story short, you for loop should be
for a=[0:180, 179:-1:-180]

Related

How to assign -INFINITY amongst others to multiple variables?

I have this line of pseudocode that I am trying to translate in Matlab:
(maxSum, maxStartIndex, maxEndIndex) := (-INFINITY, 0, 0)
I have translated the second and third variables simply assigning a 0:
maxStartIndex=0;
maxEndIndex=0;
How should I translate this line?
maxSum= -INFINITY
I have not find reference for this.
Probably you're just looking for deal:
[maxSum, maxStartIndex, maxEndIndex] = deal(-inf,0,0)

Easy way to create and initialise a 3D table of structs in Matlab

I would like to be able to initialise a big table in matlab easily.
Say I have the bounds x, y, z = 5, 4, 3. I want to be able to make a 5x4x3 table where each element is a struct that stores count and sum. Count and sum in this struct should be 0 when initialised.
I thought it would be enough to do this:
table = []
table(5,4,3) = struct('sum', 0, 'count', 0)
And this would work for a double but not with a structure evidently.
Any ideas?
EDIT:
As another question, (bonus if you will) is there a way to force matlab to store the struct, but when you access the element (i.e., table(1, 2, 3)) get it to return the average (i.e., table(1,2,3).sum/table(1,2,3).count).
Its not vital to the question but it would certainly be cool.
You'll need just to replace the line table = [] to avoid the error, that is
clear table;
table(5,4,3) = struct('sum', 0, 'count', 0)
works fine. Note, however, that this command only initializes one field of your array, i.e., the memory allocation is incomplete. To initialize all fields of your array, you can use
table2(1:5,1:4,1:3) = struct('sum', 0, 'count', 0)
to visualize the difference, use whos, which returns
>> whos
Name Size Bytes Class Attributes
table 5x4x3 736 struct
table2 5x4x3 8288 struct
Your second question can be solved, for instance, by using anonymous functions
myMean = #(a) a.sum./a.count; %define the function
myMean(table2(2,2,2)) % access the mean in the field (2,2,2)

MATLAB, how to evaluate multiple indices in one line?

I don't know how to explain this better than by giving you an example.
Suppose I have the following array:
a = magic(6)
And then I take a 'slice' of that like this:
a(:,1)
It will print:
35
3
31
8
30
4
Now I want the first number, so I want to write:
a(:,1)(1)
Instead of:
b = a(:,1)
b(1)
Also, is there a way to do something like this (assignment and comparison, i.e. set b, then evaluate against it):
(b = a(:,1))(1)
Ok, here's an update with a function where it isn't trivial to use a(1, 1)
come_on = sprintf('%i, ', magic(3));
come_on(1:end-2)
8, 3, 4, 1, 5, 9, 6, 7, 2
Also, what if I only want the first 4 numbers on magic(3)?
It would be better to write
sprintf('%i, ', magic(3)(1:4))(1:end-2)
instead of tens of lines, MHO.
You cannot concatenate indexing as foo(1)(2)(3). However, you can index multiple dimensions at once. So in this case, a(1,1) will give you what you want.

matlab search a matching element

I have an integer array of length 2000 elements. For ex
x = [2, 4, 5, 6, 5,6,7,5......];
Now in this array i need to find an element which occurs repeatedly. For ex I need to know how many times a number '5' has been occurred. In the above example it is three times.
Is there any way to search an matching element and returns the count in matlab?
Do you know the number in advance?
If so, to work out how many times it appears in x you could do:
sum(x==5)
The x==5 creates a vector of [FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE ...], being TRUE whenever x is 5.
The sum then adds up that vector, where FALSE maps to 0 and TRUE to 1.
A quick way get the count is
sum(x == 5)
If you need the indicies of the matching elements:
find(x == 5)
Notice, the count is also length(find(x == 5)).
Standard caveats apply toward the use of == and floating point numbers.

MATLAB: how do I return entries in a vector?

Let's say a=[5;4;3;2;1] and I want all entries > 3, so I want it to spit out v=[5,4].
I know "find" only finds the indices, so it doesn't exactly work.
any suggestions?
Include the inequality test in the index:
v = a(a>3)