use part of value of A1 to formula in cell B1 - libreoffice

EDIT: In my cell A1 has string value - 44 and I want to use 44 in a calculation =44*2.
How do I get the 44 from the end of cell A1?

Related

How to match and copy time-data from one matrix to another?

In MATLAB (R2015a), I have two large matrices that can be simplified as:
A = [ 24 10 830; 24 15 830; 150 17 945; 231 40 1130; 231 45 1130]
(note that in A, column 3 is the time for the event cataloged in column 1) and
B = [24 13; 150 29; 231 43]
How can I match and copy the time-data from column 3 in matrix A, to a matching and filtered event column in matrix B?
For example, I want the value 24 from first column in B to be matched with value 24 from first column in A, and then copy the corresponding time-data in A's third column (for 24 it's 830, for 150 it's 945 etc.) into B. This should result into our new B with the time-data from A:
B = [24 13 830; 150 29 945; 231 43 1130]
I relatively new to MATLAB so any help is much appreciated!
First find the location of the elements in the first row of B in the first row of A using the ismember function. And then use those locations to construct the new matrix.
[~,Locb] = ismember(B(:,1),A(:,1));
Bnew = [B A(Locb,3)]
Bnew =
24 13 830
150 29 945
231 43 1130
This is a fast way that comes to my mind. There might be some singularities that needed to be checked more thoroughly.

How to use the 'if' statement in matlab?

I have a cell array of size 5x5 as below
B= 00 10 11 10 11
01 01 01 01 11
10 00 01 00 01
10 10 01 01 11
10 10 10 00 10
And two column vectors
S1= 21
23
28
25
43
S2= 96
85
78
65
76
I want to create a new cell array of the same size as B say 5x5 such that it satisfies the following condition
Final={S1 if B{i}=11
S1 if B{i}=10
S2 if B{i}=01
S2 if B{i}=00
So the resulting output would be something like this
Z = s2 s1 s1 s1 s1
s2 s2 s2 s2 s1
s1 s2 s2 s2 s2
s1 s1 s2 s2 s1
s1 s1 s1 s2 s1
ie Z= 96 21 21 21 21
85 85 85 85 23
28 78 78 78 78
25 25 65 65 25
43 43 43 76 43
I tried using the if condition but i get error saying
'Error: The expression to the left of the equals sign is not a valid target for an assignment.'
for i=1:1:128
for j=1:1:16
if fs{i,j}=00
Z{i,j}=S1{i,j}
elseif fs{i,j}= 01
Z{i,j}=S2{i,j}
elseif fs{i,j}= 10
Z{i,j}=S1{i,j}
elseif fs{i,j}= 11
Z{i,j}=S2{i,j}
end
end
I think I'm making a mistake in the if statement as well as the expressions I'm using. Where am i going wrong? Please help thanks in advance.
Use == for comparison and = for assignment. So if fs{i,j}==00, etc.
Edit: Matlab is really designed for highly vectorized operations. Nested loops are slow compared to native functions, and typically can be replaced with vectorized versions. Is there any particular reason why you are using cell arrays instead of matrices, especially when you only have numeric data?
If B, S1, and S2 were matrices your code could be written in one highly efficient line that will run much much faster:
Z = bsxfun(#times, S1, B == 11 | B == 10) + bsxfun(#times, S2, B == 01 | B == 0)
Since B is a cell array you will want to convert it to a matrix using cell2mat unless you'd like to use cellfun.
Instead, you can just call B_mat = cell2mat(B), followed by (B_mat>=10).*repmat(S1,1,5) + (B_mat<10).*repmat(S2,1,5).
It's possible that your cell array actually contains binary values, possibly represented as strings, in which case the conditions used above would need to be changed. Then using cellfun may be necessary.

Create new variable that recognizes changes in a code in Matlab

I have a double matrix B that has in C1 the year 1998 and in C2 a code (unrepeated). Each C2 is given a value in C3.
% C1 C2 C3
B=[ 1998 22 37
1998 34 7
1998 76 12
1998 98 29
1998 107 14
…]
This is how I got to B:
N1=[N{:,1} N{:,2} N{:,3}];
N1=unique(N1(:,1:3),'rows');
N3= unique(N1(:,1:2),'rows');
for m=1:size(N3,1)
N3(m,3)=sum(N1(:,1) == N3(m,1) & N1(:,2)==N3(m,2));
end
B=N3((N3(:,1) == 1998),:);
I have a cell-array A with the years horizontally disposed in R1, un-repeated values in Y, and corresponding codes in the columns that follow. The codes match the ones in C2 from variable B.
A={Y 1996 1997 1998 1999 %R1
1 107 107 22 22
13 98 98 76 1267
… }
Is there a way I could get a new variable that recognizes the change in the codes in variable A, and presents the corresponding values from C3 in B? For instance:
AB={Y Initial C2 Change C2
1 107 14 22 37
13 98 29 76 12 }
AB = {'Y' 'Initial' 'C2' 'Change' 'C2'}; % initialize result cell array
for i=2:size(A,1) % loop through rows of A
indicesOfChanges = find(diff([A{i,2:end}])); % find changes in row
for j=1:length(indicesOfChanges) % for all changes look up the corresponding values
ind1=find(B(:,2)==A{i,indicesOfChanges(j)+1}); % row index in B of value before change
ind2=find(B(:,2)==A{i,indicesOfChanges(j)+2}); % row index in B of value after change
AB{end+1,1} = A{i,1};
AB{end,2} = B(ind1,2);
AB{end,3} = B(ind1,3);
AB{end,4} = B(ind2,2);
AB{end,5} = B(ind2,3);
end
end
Maybe this could be further improved by using a vectorized approach, but as long as your arrays aren't too big it should work fast enough using loops.

How can I concatenate vector and cell in MATLAB?

I have matrix F of dimension 5 X 3. For example:
F= [1 12 13;
2 23 24;
3 34 35;
4 45 46;
5 56 57]
and I have a label cell of size 1X1 with entry 'v' i.e.
>> label
label =
'v'
and size of F is given by :
>> [m n]=size(F)
m=
5
n =
3
I want my output to look like:
>> F
F =
1 12 13 v
2 23 24 v
3 34 35 v
4 45 46 v
5 56 57 v
How can I concatenate the cell with the matrix to get this output?
To create an array that contains both numeric and non-numeric data, you need to put everything into a cell array (replace label by {label} in case it isn't a cell array):
Fcell = [ num2cell(F), repmat(label,size(F,1),1)]
You can then access individual numbers/letters using the curly brackets:
Fcell{2,2}
ans =
23
As #Jonas described, converting it to cells is the way to go when you want to access the data for further use.
However, if you are solely interested in seeing the data on the screen and don't like the brackets this is also an option:
Fcell = [num2str(F) repmat([' ' label{1}],size(F,1),1)]
If your label is actually a char it should work like this:
Fcell = [num2str(F) repmat([' ' label],size(F,1),1)]

find sorting index per row of a 2D matrix in MatLab and populate a new matrix

I have a challenge to order my matrix. The provided functions like sortrows work in the opposite way...
Take this 2D matrix
M =
40 45 68
50 65 58
60 55 48
57 67 44
,
The objective is to find matrix O that indicates the sorting index (rank) per row, i.e.:
O =
1 2 3
1 3 2
3 2 1
2 3 1
.
So for the second row 50 is the smallest element (1), 65 the largest (3), and 58 is the second largest (2), therefore row vector [1 3 2].
[~,sorted_inds] = sort(M,2);
will do.
I think you're looking for the second output of the regular sort function:
[~,I] = sort(M,2)
This syntax supresses the actual sorted matrix Msorted, and returns the indices I such that
for j = 1:n, Msorted(j,:) = M(I(j,:),j); end
Type doc sort for more information.