Get all values in one vector in Matlab - arrays

I have two arrays, A (500 x 128 integer values) and B (500 x 64 real values). I want to concatenate both to get C. The problem is that Matlab ignores all values in B as they are small values. Is there any way to get all values without neglect?
Thanks.

I think this can simulate your problem:
A = int8(randi(4,4)*10);
B = rand(4,4)*10;
C = [A B]
C =
10 20 20 30 3 0 8 3
40 10 40 40 2 6 1 2
30 20 10 30 2 1 6 6
40 20 40 30 9 9 5 5
To achieve the result you want, you have to add a type to your data before concatenating them:
C = [double(A) B]
C =
Columns 1 through 7:
10.00000 20.00000 20.00000 30.00000 2.92979 0.31162 7.73694
40.00000 10.00000 40.00000 40.00000 1.71392 5.82900 1.08936
30.00000 20.00000 10.00000 30.00000 1.83903 0.84160 5.75773
40.00000 20.00000 40.00000 30.00000 8.81039 9.31400 4.60636
Column 8:
3.10192
1.75853
5.75013
5.39383
So here when you mention that A has to be shown as a double matrix, the other matrix, B is kept at its original type. You can check the other existing types in MATLAB here.

Related

How to find minimum value of a column imported from Excel using MATLAB

I have a set of values in the following pattern.
A B C D
1 5 6 11
2 6 5 21
3 7 3 42
4 3 7 22
1 2 3 54
2 3 2 43
3 4 3 27
4 3 2 14
I exported the every column into MATLAB workspace as follows.
A = xlsread('F:\R.xlsx','Complete Data','A2:A43');
B = xlsread('F:\R.xlsx','Complete Data','B2:B43');
C = xlsread('F:\R.xlsx','Complete Data','C2:C43');
D = xlsread('F:\R.xlsx','Complete Data','D2:D43');
I need help with code where the it has to check the Column A, find the lowest D value and output the corresponding B and C values. I need the output to look like.
1 5 6 11
2 6 5 21
3 4 3 27
4 3 2 14
I read through related questions and understand that I need to make it a matrix and sort it based on the element on the 4th column using
sortrows
and get indices of the sorted elements. But I am stuck here. Please Guide me.
You can export those columns in one go as:
ABCD = xlsread('F:\R.xlsx','Complete Data','A2:D43');
Now use sortrows to sort the rows according to the first and the fourth column.
req = sortrows(ABCD, [1 4]);
☆ If all elements of the first column exist twice then:
req = req(1:2:end,:);
☆ If it is not necessary that all elements of the first column will exist twice then:
[~, ind] = unique(req(:,1));
req = req(ind,:);

Combining two matrices of different size based on condition in MATLAB

I have two matrices, A of size 1x30974 and B of size 55x30974. Matrix A contains values from 1 to to 30974, while matrix B (first row) contains values that are also elements of matrix A, yet they do not have to be in order.
So in a simple case, I would have:
A = [1 2 3 4 5 6 7 8];
B = [1 2 6 8; 20 21 22 23; 30 31 32 33];
I would like to compare A and B in a way that my output would return:
C = [1 2 3 4 5 6 7 8; 20 21 NaN NaN NaN 22 NaN 23; 30 31 NaN NaN NaN 32 NaN 33];
Saying differently, if the value in the first row of B is an element of A, then return all values in this column. If an element of A has no value in the first row of B, then the column is NaN.
In my case, the output would be of size 55x30974.
I guess that ismember could be the function I am looking for, but even then, how could I get the values of the column?
You should use both outputs from ismember. The first tells you if a value is present and the second gives you the index where it is found (or 0 if it isn't present):
[isMatch, index] = ismember(B(1, :), A);
C = nan(size(B, 1), numel(A));
C(:, index(isMatch)) = B(:, isMatch);

Matlab replace element between 2 matrix

I have two matrix M x N, For simplicity, we take 4x4:
Matrix A:
1 4 2 5
4 5 8 2
3 4 5 6
2 3 5 8
Matrix B:
10 11 12 13
56 11 23 45
34 44 33 25
25 63 35 78
If an element of matrix A is greater then 5, then we change it from matrix B.
At the end we must get a matrix C:
1 4 2 5
4 5 23 2
3 4 5 25
2 3 5 78
How can I make it, should I use something like logical indexing..
Yes, you should use logical indexing:
C = A;
C(C>5) = B(C>5);
This means that every element in C that is >5 is set to the corresponding value in B.
or
C = A.*(A<=5) + B.*(A>5);
The comparisons in the parentheses create arrays with 0 and 1, so the first multiplication sets all elements of A to zero that should be taken from B, and second multiplication sets all elements of B to zero that should be taken from A.

calculation of value from given matrix

suppose that we have following array :
a=[12 21 23 10 34 54 10 9 5 6 7 8]
a =
12 21 23 10 34 54 10 9 5 6 7 8
length(a)=
length(a)
ans =
12
now i want to create following vector b ,which b(1),b(2)...b(6) are following
b(1)=sqrt(a(1)^2+a(2)^2)
b(2)=sqrt(a(3)^2+a(4)^2)
b(3)=sqrt(a(5)^2+a(6)^2))
b(4)=sqrt(a(7)^2+a(8)^2)
b(5)=sqrt(a(9)^2+a(10)^2))
b(6)=sqrt(a(11)^2+a(12)^2)
i have wrote following code
or i=2:2:length(a)
b(i/2)=sqrt(a(i-1)^2+a(i)^2);
end
>> b
b =
24.1868 25.0799 63.8122 13.4536 7.8102 10.6301
but i am not sure if it is correct,pleas help me to clarify if everything is ok in my code
In matlab, loops are quite slow. Using vectors is much faster. I suggest therefore a solution without a loop:
a_1 = a(1:2:end);
a_2 = a(2:2:end);
b = sqrt(a_1.^2 + a_2.^2);
first, you create a vector a_1 containing all elements with odd indices of a and a vector a_2 containing all elements with even indices.
Then you square them element wise (.^) and take the square of the sum.
For you example of a, this is 75 times faster. As you increase the size of the array, you will save even more time.

Associating / linking an array column with another column in the array

I have an array that has some calcultations done on the second column. I would like the values from the third column to follow/be linked to the second column.
Test Code:
a1= [1,10,-11;
2,70,232;
3,33.2,-33;
4,40,44;]
a2calc=abs(a1(:,2)-max(a1(:,2))) %calculation
a2=[a1(:,1),a2calc,a1(:,3)] %new array
Example:
original a1 Array
1 10 -11
2 70 232
3 33.2 -33
4 40 44
a2 Array after column 2 calculations looks like this
1 60 -11
2 0 232
3 36.8 -33
4 30 44
I'm trying to get the final array to look like this (column 3 values follow / are linked to the second column)
1 60 232
2 0 -11
3 36.8 44
4 30 -33
What I'm having problems with is I'm not sure if I should use the index values of column 2 and if so how I can get it to look like the final output array I included in the question.
I might be wrong here, but it looks to me like the logic is:
After calculating the second column, change the order of the third column so that the third column is sorted the same way as the second. To see what I mean:
This represents the two columns, numbered from highest to lowest:
A = 1 1
4 3
2 2
3 4
If I understand it right, you want the resulting matrix to be
A = 1 1
4 4
2 2
3 3
If this is the right logic then you should check out sort with two outputs. You can use the second output to index the third column.
[~, idx] = sort(A(:, 2));
sorted_3 = sort(A(:, 3));
A(idx, 3) = sorted_3;
The output from this is:
A =
1.00000 60.00000 232.00000
2.00000 0.00000 -33.00000
3.00000 36.80000 44.00000
4.00000 30.00000 -11.00000
Good luck!

Resources