Operation on Matlab arrays - arrays

I'm quite new to Matlab and I was trying to figure out a line in a code I'm trying to review and understand.
data(:,U==0)=[];
To give some context :
The class of the variable data is a double, and inside of it I have an array (matrix) of numbers :
For exemple :
data = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
U is a double too, given by the standard deviation of the data :
U=std(data);
For the same exemple :
U = [4.7809 4.7809 4.7809 4.7809]
The question I am asking is about the first line of code I shared at the beginning, I think I understood a bit how arrays were working on Matlab, but I can't find any real documentation about what the "U==0" means here.
So what I'm really asking is how this line of code will affect my dataset if my variable "data" contains a lot more data in it?
If you have some piece of documentation to further understand the concepts behind this kind of operations on matrixes and arrays in Matlab I would be really grateful if you would share them with me.
Thank you so much in advance if you have some kind of solution or explanation.
Have a great day !

Related

How to process array data in chunks where last chunk may be a partial size

I have array of integer and I am trying to send this array as a sub block from esp32 to another one.
According this code I get on output like this:
output:
1 2 3 4 5
6 7 8 9 10
11 12 0 0 0
the expected output:
1 2 3 4 5
6 7 8 9 10
11 12
How can I update on esp_now_send to get like the expected output? how can I deal with the last sub block if it is less than 5 numbers?
The code needs to send up to only the available data. To do that the general approach would be to send full sub-blocks until the last sub-block which may be a partial one. That can be determined by simple maths logic to work out how much the current iteration should send based on how much data is left.
The code changes would be:
Change siz to be the real number of entries in the array: siz = sizeof(data)/sizeof(data[0]).
Change rang in the function call to `(ind + rang <= size ? rang : size - ind)``. That is, the size passed to the function call depends on how much data is left.

Mean values of a matrix in a matrix on Matlab

This is about matlab.
Let's say I have a matrix like this
A = [1,2,3,4,5;6,7,8,9,10;11,12,13,14,15]‍
Now I want to know how to get a mean value of a small matrix in A.
Like a mean of the matrix located upper left side [1,2;6,7]
The only way I could think of is cut out the part I want to get a value from like this
X = A(1:2,:);
XY = X(:,1:2);
and mean the values column wise Mcol = mean(XY);.
and finally get a mean of the part by meaning Mcol row-wise.
Mrow = mean(Mcol,2);
I don't think this is a smart way to do this so it would be great if someone helps me make it smarter and faster.
Your procedure is correct. Some small improvements are:
Get XY using indexing in a single step: XY = A(1:2, 1:2)
Replace the two calls to mean by a single one on the linearized submatrix: mean(XY(:)).
Avoid creating XY. In this case you can linearize using reshape as follows: mean(reshape(A(1:2, 1:2), 1, [])).
If you want to do this for all overlapping submatrices, im2col from the Image Processing Toolbox may be handy:
submatrix_size = [2 2];
A_sub = im2col(A, submatrix_size);
gives
A_sub =
1 6 2 7 3 8 4 9
6 11 7 12 8 13 9 14
2 7 3 8 4 9 5 10
7 12 8 13 9 14 10 15
that is, each column is one of the submatrices linearized. So now you only need mean(A_sub, 1) to get the means of all submatrices.

matlab making repetitions of data from one array into another array [duplicate]

This question already has answers here:
Element-wise array replication in Matlab
(7 answers)
Closed 6 years ago.
I would appreaciate a lot if you help. I am beginner in programming. I am using Matlab. So, I have an array which is 431x1 type - double; there i have numbers 1 to 6; for ex: 1 4 5 3 2 6 6 3 3 5 4 1 ...; what I want to do is I need to make a new array where I would have each element repeat for 11 times; for ex: a(1:11)=1; a(12:22)=4; a(23:33)=5; or to illustrate differently : a=[1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4...];
I tried doing it in a loop but had some problems, which way could you suggest, do you know any function I could take advantage of?
First of all, it would help if you could format your code is separate blocks to make your question easier to read...
Let's say you had an array of length Nx1 as:
x = [1 2 3 4 5 ...]';
You could construct a loop and concatenate as:
for i = 1 : length(x)
for i = 1: length(x)
y(1 + (i - 1) * 11 : 1 + i * 11) = x(i); % Copy to a moving block
end
y(end) = []; % Delete the superfluous one at the end
You could also look at functions like repmat in the MATLAB help for replicating arrays.
Try this (NRepis how many times you want it repeated):
x = [1, 2, 3, 4, 5];
NRep = 5;
y = reshape(repmat(x,[NRep,1]),[1,length(x)*NRep])
Since it's a little cumbersome to write that out, I also particularly enjoy to use this "hack":
x = [1, 2, 3, 4, 5];
NRep = 5;
y = kron(x, ones(1,NRep));
Hope that helps!
P.S.: This is designed for row vectors only. Though if you need column vectors it's easy to modify.
edit: Of course, if you're post-R2015a you can just use y=repelem(x,NRep). I tend to forget about those because I work on older Matlabs (and sometimes it's not such a bad idea to be a bit backward compatible). Thanks to #rahnema1 for reminding me.

MATLAB: Return the largest number in an array?

I'm new to MATLAB (and this website!) and I needed some help with a problem I had been assigned for class. I searched this website for similar MATLAB problems, but I didn't come across any. The problem is asking the user to return the biggest number which is next to a zero. In other words, write a function which takes a list/array of numbers as input and returns the largest number which is adjacent to a zero. For instance, if
a=[1 -2 3 4 0 5 6 0 -7], Output: y=6.
I tried to solve the problem using a somewhat complex function I found online, and it seems to work on MATLAB. However, it won't work on our automated online MATLAB grading system as the command "imdilate" isn't recognized:
x=[1 2 0 4 5 -6 0 7 0 8]
zero_mask = (x == 0);
adjacent_to_zero_mask = imdilate(zero_mask, [1 0 1]);
max_value_adjacent_to_zero = max(x(adjacent_to_zero_mask));
y=max_value_adjacent_to_zero
I wanted to ask, is there is much simpler method of solving this problem not involving "imdilate" or other similar functions?
Thank you for your help, I really appreciate it!
I came up with a dirty solution:
a=[0 1 -2 3 4 0 5 6 0 -7];
I=find(a==0);
I=unique([I+1,I-1]);
I=I((I>0)&(I<=length(a)));
output = max(a(I));

Matlab Array removing zero value

I am new to MatLab and programming in general. I have been set the following problem:
4 players take part in a competition each person starts with 100 points.
Each person randomly plays another player.
If a player wins they get 1 point and the losing player loses 1 point from their total.
When a player has zero points they are elimated.
The game is over when there is only 1 player left.
I assume I need to create some sort of matrix array with 4 players and their 4 scores
A = [1 2 3 4; 100 100 100 100]
Is this correct and how would i remove a player and their score once it reaches zero.
Any help would be greatly recieved. Thanks
I think you are on the right track, assuming you define A as such, you can remove the players with nonpositive score as such:
A = [1 2 3 4; 100 100 0 100]
idx = A(2,:)<0.01 %To prevent suffering from rounding errors
A = A(:,~idx)
I just came across this page while looking for this answer myself (I'm also new to Matlab).
However, the above answers seem too complicated; Matlab's matrix addressing and assignment operators solve this problem very simply, as I found out after a bit of experimentation:
A = [1 2 3 4; 100 0 100 100]
A =
1 2 3 4
100 0 100 100
A(:,A(2,:)==0)=[]
A =
1 3 4
100 100 100
I hope this is useful to someone.
The 1st answer is absolutely correct. However, in order to approach this problem, there is a straight-forward syntax built for this purpose, using sparse you will also be able to extract the player with 0-points directly:
A = [1 2 3 4; 100 100 0 100]
ans= sparse (A(2,:))
Good luck ;)

Resources