Copy matrix rows matlab - matlab

Lets say i have a matrix A of 300x65. the last column(65th) contains ordered values (1,2,3). the first 102 elements are '1', the second 50 elements are '2' and the remainder will be '3'.
I have another matrix B, which is 3x65 and i want to copy the first row of B by the number of '1's in matrix A. The second row of B should be copied by the number of '2's in in matrix A and the 3th row should be copied by the remaining value of matrix A. By doing this, matrix B should result in a 300x65 matrix.
I've tried to use the repmat function of matlab with no succes, does anyone know how to do this?

There are many inconsistencies in your problem
first if you copy 1 row of B for every element of A(which will end up happening by your description) that will result in a matrix 19500x65
secondly copy its self is a vague term, do you mean duplicate? do you want to store the copied value into a new var?
what I gathered from your problem is you want to preform some operation between A and B to create a matrix and store it in B which in itself will cause the process to warp as it goes if you do not have another variable to store the result in
so i suggest using a third variable c to store the result in and then if you need it to be in b set b = C
also for whatever process you badly described I recommend learning to use a 'for' loop effectively because it seems like that is what you would need to use
syntax for 'for' loop
for i = [start:increment:end]
//loops for the length of [start:increment:end]
//sets i to the nth element of [start:increment:end] where n is the number of times the loop has run
end

If I understand your question, this should do it
index = A(:,end); % will be a column of numbers with values of 1, 2, or 3
newB = B(index,:); % B has 3 rows, which are copied as required by "index"
This should result in newB having the same number of rows as A and the same number of columns as the original B

Related

Deletion of all but the first channel in a cell of matrices

I have a row cell vector M, containing matrices in each cell. Every matrix m (matrix inside the big matrix M) is made of 2 channels (columns), of which I only want to use the first.
The approach I thought about was going through each m, check if it has 2 channels, and if that is the case delete the second channel.
Is there a way to just slice it in matlab? or loop it and obtain the matrix M as the matrix m would disappear.
First code is:
load('ECGdata.mat')
I have the below.
when I double-click in one of the variable , here is what I can see:
As you can see the length of each matrix in each cell is different. Now let's see one cell:
The loop I'm trying to get must check the shape of the matrix (I'm talking python here/ I mean if the matrix has 2 columns then delete the second) because some of the variables of the dataframe have matrix containing one column (or just a normal column).
In here I'm only showing the SR variable that has 2 columns for each matrix. Its not the case for the rest of the variables
You do not need to delete the extra "channel", what you can do is quite simple:
newVar = cellfun(#(x)x(:,1), varName, 'UniformOutput', false);
where varName is SR, VF etc. (just run this command once for each of the variables you load).
What the code above does is go over each element of the input cell (an Nx2 matrix in your example), and select the first column only. Then it stores all outputs in a new cell array. In case of matrices with a single column, there is no effect - we just get the input back.
(I apologize in advance if there is some typo / error in the code, as I am writing this answer from my phone and cannot test it. Please leave a comment if something is wrong, and I'll do my best to fix it tomorrow.)

Creating new rows in a matrix, where the elements of these rows have to be different from previous rows(same row cannot occur twice) Matlab

I'm trying to generate a 3x2 matrix. Each row is generated using randperm(3,2). This means each row is generated as a vector with 2 unique integers with values between 1 and 3.
The problem is that I want each new row to be different than all the previous. For example, if one row is [1 3] then no other row can be:
[1 3], nor
[3 1].
I tried checking the sum AND the multiplied value of each newly created row. (Using our example 1+3=4 and 1*3=3)
My idea is that the multiplied value and the sum value of each new generated row is compared to the multiplied value and sum value of every other row that comes before it. If any of these values are the same (which means we will get a repetition), we keep generating a new row using randperm(3,2) until a completely new row is obtained.
My code checks each each row before one at a time, and "forgets" every other row that it previously checked. It does not take into consideration ALL the previous rows, instead it only iterates back one step at a time. I tried using something like parents(i:-1:1) instead of parents(i-k,1) etc but couldn't make it work.
Question: How can I do this comparison?
parents=randperm(3,2);
for i=2:3
parents=[parents; randperm(3,2)]
for k=1:i-1
while prod(parents(i,:))==prod(parents(i-k,:)) && sum(parents(i,:))==sum(parents(i-k,:))
parents(i,:)=randperm(3,2)
end
end
i=i+1;
end
Download this function, then use it as follows:
% generate all the possible permutations
p = permn(1:3,2);
% generate a random permutation of the indices and take the first three
r = randperm(size(p,1));
idx = r(1:3);
% take three random rows from the possible permutations using the indices
result = p(idx,:);
This way:
you will never obtain a row identical to another one in your result matrix
you won't be forced to use a "shuffle until condition is met" approach
you have a reusable and flexible approach

I want to extract a row element in a matrix

X =
4 3
8 3
I want to extract the element in each row of X and do some operation on each of them separably (4,3) and (8,3). however the size of may be different based on some parameters in my code, so I want general formula to do such thing,
How I can use the for loop for solving this issue ?
This link shows how to extract specific lines (or columns) from a matrix http://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
All you have to do is write a loop on an index ii to go through every line.
for ii=1:size(X,1)
a=myfun(X(ii,:));
end

Assigning the different row to another matrix after comparing two matrices

i have two matrices
r=10,000x2
q=10,000x2
i have to find out those rows of q which are one value or both values(as it is a two column matrix) different then r and allocate them in another matrix, right now i am trying this.i cannot use isequal because i want to know those rows
which are not equal this code gives me the individual elements not the complete rows different
can anyone help please
if r(:,:)~=q(:,:)
IN= find(registeredPts(:,:)~=q(:,:))
end
You can probably do this using ismember. Is this what you want? Here you get the values from q in rows that are different from r.
q=[1,2;3,4;5,6]
r=[1,2;3,5;5,6]
x = q(sum(ismember(q,r),2) < 2,:)
x =
3 4
What this do:
ismember creates an array with 1's in the positions where q == r, and 0 in the remaining positions. sum(.., 2) takes the column sum of each of these rows. If the sum is less than 2, that row is included in the new array.
Update
If the values might differ some due to floating point arithmetic, check out ismemberf from the file exchange. I haven't tested it myself, but it looks good.

matlab Attempted to access rel(9508,4); index out of bounds because size(rel)=[9507,8]

I have a matrix rel nx8 and a second matrix mx3. I want to check if column 3 of matrix rel contains an 8. If yes, I pass to the second condition: if column 3 of matrix rel is equal to to column 2 of matrix A, go to the third condition and create two additional columns in matrix A with the elements of columns 7 and 8 of matrix rel.
n=size(rel)
m=length(A)
for i=1:n
for k=1:m
if rel(i,4)==8
if rel(i,3)==A(k,2)
if (rel(i,2)== A(k,1)) || (rel(i,1)== A(k,1))
A(k,4)=rel(i,7);
A(k,5)=rel(i,8);
end
end
end
i=i+1
end
end
However I get this message error: Attempted to access rel(9508,4); index out of bounds because size(rel)=[9507,8].
Does anybody know how could I fix it?
It is because you are iterating your variable i inside of your k for loop. So when you arrive in your k loop you are stepping up your original i from the first loop to i+m. This is the source of your error, as i+m becomes greater than n.
To see this, gut out your if statements, set n and m to some reasonable values (like 2 and 3) and just run the loops, keeping track of what happens to i.