Arrays in matlab - arrays

I have a structure names eye_record which has 6 fields, one of which is x_pos_measured_deg:[1800x1 double]
I want to declare an array in such a way that using for loop, i can get all values of that specific field into a new array and do some work on them. Can anyone show me how to do that? here is m code:
arr=zeros(1,1800);
for t=1:length(eye_record);
arr(t)= eye_record(t).x_pos_measured_deg;
end
it gives me this error: In an assignment A(I) = B, the number of elements in B and I must be the same. How can i fix this? or how should i declare my array so that it won't give me this error? I want all the objects or values, which are in x_pos_measured_deg field to go into my new array.

Your eye_record is struct, not array, so you can not use indexing with eye_record. Your eye_record.x_pos_measured_deg is array and you have to loop through it. So the loop should be:
arr=zeros(1,1800);
for t=1:length(eye_record.x_pos_measured_deg)
arr(t)= eye_record.x_pos_measured_deg(t);
end
But actually, you can assign values directly like:
arr=zeros(1,1800);
arr = eye_record.x_pos_measured_deg';
since you declared arr to have size of 1x1800, and eye_record.x_pos_measured_deg has size of 1800x1.
Without arr=zeros(1,1800);, then no ' at the end:
arr = eye_record.x_pos_measured_deg;

Related

VB.Net create as many arrays as possible from one array

I have an Array lets say myarray ={1,2,3,4,5,6}
What I'm trying to do:
is generate as many as new arrays by changing each value with a value of the same array
example: myarray[0]=myarray[1] so first array will be {2,2,3,4,5,6}
then myarray[0]=myarray[2] so 2nd array will be {3,2,3,4,5,6}
later myarray[1] = myarray[0] so array will be {1,1,3,4,5,6}
and so on
I tried to think of many solutions but none of them work.

Manipulating a 'dynamic' array in javascript

As part of my nightwatchjs testing script, I have a 'dynamic' array that I would like to add a character to.
So, at the moment my array has a value of 4000, but I would like this to read 4,000.
This means that I need to add a , to my array.
The issue I have however, is that this value could change the next time I run the test script, so it could be 10000 or 100000.
So I suppose what I'm asking is whether it's possible to "select a value 3 elements from the end of my array?"
So no matter what or how many elements are in the array, the array will read xx,000.
Any help would be much appreciated. Many thanks.
Does this array have a single value? If so, why is it an array instead of just a variable?
You can use toLocaleString() to add commas to a numeric value, but it will return a string and not a number.
let arr = [4000]
let num = arr[0]
let commas = num.toLocaleString()
console.log(commas)
// "4,000"

How to assign a arrays values to a multi dimensional array in VBA

I have a multidimensional array that is setup to hold a "identity number" as the first part of the array, and then a set of values as the second part. Ie
Dim holder(8,2) as integer
I'm trying to make holder(1) equal to a already defined array, as it is to many different values to write
Instead of
Holer(1,1) = 1
Holder(1,2) =2
Etc
Do this
Holder(1) = Array(1,1,1)
Normally this wouldn't be a issue but I have multiple arrays
Ie
Holder(1) = Array(1,1,1)
Holder(2) = Array2(1,1,1)
How do I do this?
Another way to word it is put the values of array a into the holder array 1.

Julia: 2d array assignment

I'm trying to do the assignment in the 2d array with the nested loop. I'm trying to access the elements of the array as follows. But I get a mistake. I've searched, but I didn't get results. How can I assign Julia in the 2d array?
for x in 1:total
for y in 1:W
#show (x, y)
if agirliklar[x] <= y
V[x][y] = getMax(V[x-1][y], degerler[x] + V[x-1][y - agirliklar[x]])
else
print("sa")
V[x][y] = V[x-1][y]
end
end
end
BoundsError: attempt to access 7×6 Array{Int64,2} at index [0]
My code
Error
In Julia arrays are 1-based not 0-based.
You try to access V[x-1] where x can take value of 1.
Site note: always provide a minimum working example (MWE) rather than just dumping a part of your production code.
(At least) two things are wrong here:
As #PrzemyslawSzufel says, ordinary Julia arrays are 1-indexed, so you cannot access them at index zero. Though it is possible to get special arrays that are 0-indexed.
If V is a 2D array, as you are saying, you cannot access it like this: V[x][y]. Instead you access them like this: V[x, y]. You can read more about this here: https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1

How do I reassign even and odd indices of a character array into a new smaller character array in Matlab?

In matlab I have a 32x1 character array A such that
A = {'F1' 'F2' 'F3' 'F4' 'F5' 'F6' ... 'F32'};
A = A';
Now I am trying to do the following with A.
For every even index of A meaning
A{2}, A{4}, A{6}...
I want to assign those values to a 16x1 character array B and for the odd indices of A I want to assign those values to a different 16x1 array C.
I use the following code:
for i=1:32
if mod(i,2)==0
B{i} = A{i};
else
C{i} = A{i};
end
end
and it works, but only partially because it assigns the right values at for e.g. B{2} and B{4} but the values in B{1} and B{3} are the same as in B{2} and B{4}.
Can anybody tell me how to reassign even and odd indices of a character array into a new smaller character array? My problem is that I am going from a 32x1 into a 16x1 and I'm not sure how to avoid the extra 16 entries.
Many thanks!
To get this question actual answered, use the idea of Luis Mendo in the comments. You can combine it with deal to save one line of code:
[B, C] = deal(A(2:2:end), A(1:2:end))
To make your loop work, you need a second running index jj:
A = {'F1' 'F2' 'F3' 'F4' 'F5' 'F6'};
for ii = 1:6
jj = ceil(ii/2);
if mod(ii,2)==0
B{jj} = A{ii};
else
C{jj} = A{ii};
end
end

Resources