Update addition depending to size array - matlab

I don't find a solution to this problem
In my initialization I define an array "R" with certain number of values (the boolean flag with have the same length too). Later in my code I do an addition with a boolean flag.
Do you have an idea how to "update" this addition without editing it manually?
The part of the code i want to improve is
( (R(5)*B(i,5))+ (R(1)*B(i,1)) + (R(3)*B(i,3)) +(R(4)*B(i,4)) +(R(2)*B(i,2)) )
Thank you in advance for you answear
the code :
% i reflects the time
% bollean flag type double
B(1,:)=[0 0 0 0 0];
B(2,:)=[0 0 0 0 0];
B(3,:)=[0 0 0 0 1];
B(4,:)=[0 0 0 1 0];
B(5,:)=[0 0 0 1 1];
%info1
E(1)=0;
E(2)=0;
E(3)=10;
E(4)=20;
E(5)=40;
%info2
R = [1/30 1/30 1/30 1/30 1/30];
for i=1:5
for k=1:5
if E(i)>0
powerload_R2(i,k)= ( ( R(k))/( (R(5)*B(i,5))+ (R(1)*B(i,1)) + (R(3)*B(i,3)) +(R(4)*B(i,4)) +(R(2)*B(i,2)) ) ) *B(i,k)*E(i)+0; % fonctionnel
else
powerload_R2(i,k)= 0;
end
end
end
'end'
results
%results
powerload_R2(i,k)=
0 0 0 0 0
0 0 0 0 0
0 0 0 0 10
0 0 0 20 0
0 0 0 20 20

Your code could be greatly simplified. As #AnderBiguri has mentioned, this long line (R(5)*B(i,5))+ (R(1)*B(i,1)) + (R(3)*B(i,3)) +(R(4)*B(i,4)) +(R(2)*B(i,2)) is just the sum of the product of R elements with the corresponding elements of the ith row of B, or simply dot(R,B(i,:)).
Also you can initialize powerload_R2 = zeros(5) and alter only those rows corresponding to E > 0. This way, you only have to iterate find(E > 0) times over the rows of powerload_R2 and you don't need the inner k loop. That said, loops are not as evil these days as they used to be on the early years of MATLAB, so use the most natural way to write your algorithm before thinking about vectorization for speed.
% i reflects the time
% boolean flag type double
B = [0 0 0 0 0
0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
0 0 0 1 1];
% info1
E = [0 0 10 20 40];
% info2
R(1:5) = 1/30;
powerload_R2 = zeros(5);
for i = find(E > 0)
powerload_R2(i,:) = R ./ dot(R,B(i,:)) .* B(i,:)*E(i); % fonctionnel
end

Related

Performing an averaging operation over every n elements in a vector

I have a logical vector in which I would like to iterate over every n-elements. If in any given window at least 50% are 1's, then I change every element to 1, else I keep as is and move to the next window. For example.
n = 4;
input = [0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 1];
output = func(input,4);
output = [0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1];
This function is trivial to implement but is it possible to apply a vectorized implementation using logical indexing?. I am trying to build up the intuition of applying this technique.
here's a one liner (that works for your input):
func = #(input,n) input | kron(sum(reshape(input ,n,[]))>=n/2,ones(1,n));
of course, there are cases to solve that this doesnt answer, what if the size of the input is not commensurate in n? etc...
i'm not sure if that's what you meant by vectorization, and I didnt benchmark it vs a for loop...
Here is one way of doing it. Once understood you can compact it in less lines but I'll details the intermediate steps for the sake of clarity.
%% The inputs
n = 4;
input = [0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 1];
1) Split your input into blocks of size n (note that your final function will have to check that the number of elements in input is a integer multiple of n)
c = reshape(input,n,[]) ;
Gives you a matrix with your blocks organized in columns:
c =
0 0 0 0 0
0 1 0 1 0
0 1 0 0 0
1 0 1 1 1
2) Perform your test condition on each of the block. For this we'll take advantage that Matlab is working column wise for the sum function:
>> cr = sum(c) >= (n/2)
cr =
0 1 0 1 0
Now you have a logical vector cr containing as many elements as initial blocks. Each value is the result of the test condition over the block. The 0 blocks will be left unchanged, the 1 blocks will be forced to value 1.
3) Force 1 columns/block to value 1:
>> c(:,cr) = 1
c =
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
1 1 1 1 1
4) Now all is left is to unfold your matrix. You can do it several ways:
res = c(:) ; %% will give you a column vector
OR
>> res = reshape(c,1,[]) %% will give you a line vector
res =
0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1

reducing number of 0 rows in matrix using matlab

I have written following program in Matlab
clc;
clear all;
close all;
matrix = [ 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1;
1 1 1 0 0 1 1 1;
0 0 1 1 1 1 1 1;
0 0 0 0 0 0 0 0;
0 0 1 1 1 1 1 1;
0 0 0 0 0 0 0 0;
0 0 1 1 1 1 1 1;
1 0 0 1 1 1 1 1;
1 1 1 0 0 0 1 1];
[row,column] = size(matrix);
for i = 1:row
if matrix(i,:) == 0
matrix(i,:) = [];
end
end
disp(matrix);
what I expect is the 0 rows will be removed in the output matrix.
Where as I am getting error
Index in position 1 exceeds array bounds (must not exceed
8).
Error in Untitled (line 21)
if matrix(i,:) == 0
>>
When you delete a row from the matrix, it no longer has row rows, but row-1. Since you cannot adjust the limits of the loop, you will index out of range. Also, you will skip the row i+1 if you delete row i.
Instead, first find all the rows that need to be deleted, then delete them all at once:
index = all(matrix == 0, 2);
matrix(index,:) = [];
You can of course write that in a single line of code.
As #Cris said, the reason that you're getting an out of bounds error is that you're deleting from the beginning and shrinking the matrix before you check the end.
Another way to fix this is to simply reverse the loop, starting from the end and working toward the beginning. That way, even if you remove a row, its index will never be checked again.
for i = row:-1:1 % loop from last row to first
if matrix(i,:) == 0
matrix(i,:) = [];
end
end

How to sort the columns of a matrix in order of some other vector in MATLAB?

Say I have a vector A of item IDs:
A=[50936
332680
107430
167940
185820
99732
198490
201250
27626
69375];
And I have a matrix B whose rows contains values of 8 parameters for each of the items in vector A:
B=[0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 1 0 1 1 1
1 0 1 0 0 1 0 1 1 1
0 0 1 0 0 0 0 1 0 1
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1];
So, column 1 in matrix B represents data of item in row 1 of vector A, column 2 in matrix B represents data of item in row 2 of vector A, and so on. However, I want matrix B to contain the information in a different order of items stored in vector A2:
A2=[185820
198490
69375
167940
99732
332680
27626
107430
50936
201250];
How do I sort them, so that column 1 of matrix B contains data for item in row 1 of vector A2, column 2 of matrix B contains data for item in row 2 of vector A2, and so on?
My extremely crude solution to do this is the following:
A=A'; A2=A2';
for i=1:size(A,2)
A(2:size(B,1)+1,i)=B(:,i);
end
A2(2:size(B,1)+1,:)=zeros(size(B,1),size(B,2));
for i=size(A2,2)
for j=size(A,2)
if A2(1,i)==A(1,j)
A2(2:end,i)=A(2:end,j);
end
end
end
B2 = A2(2:end,:);
But I would like to know a cleaner, more elegant and less time consuming method to do this.
A possible solution
You can use second output of ismember function.
[~ ,idx] = ismember(A2,A);
B2 = B(:,idx);
Update:I tested both my solution and another proposed by hbaderts
disp('-----ISMEMBER:-------')
tic
[~,idx]=ismember(A2,A);
toc
disp('-----SORT:-----------')
tic
[~,idx1] = sort(A);
[~,idx2] = sort(A2);
map = zeros(1,size(idx2));
map(idx2) = idx1;
toc
Here is the result in Octave:
-----ISMEMBER:-------
Elapsed time is 0.00157714 seconds.
-----SORT:-----------
Elapsed time is 4.41074e-05 seconds.
Conclusion: the sort method is more efficient!
As both A and A2 contain the exact same elements, just sorted differently, we can create a mapping from the A-sorting to the A2-sorting. For that, we run the sort function on both and save indexes (which are the second output).
[~,idx1] = sort(A);
[~,idx2] = sort(A2);
Now, the first element in idx1 corresponds to the first element in idx2, so A(idx1(1)) is the same as A2(idx2(1)) (which is 27626). To create a mapping idx1 -> idx2, we use matrix indexing as follows
map = zeros(size(idx2));
map(idx2) = idx1;
To sort B accordingly, all we need to do is
B2 = B(:, map);
[A2, sort_order] = sort(A);
B2 = B(:, sort_order)
MATLAB's sort function returns the order in which the items in A are sorted. You can use this to order the columns in B.
Transpose B so you can concatenate it with A:
C = [A B']
Now you have
C = [ 50936 0 0 1 1 0 0 0 0;
332680 0 0 0 0 0 0 0 0;
107430 0 0 1 1 1 0 0 0;
167940 0 0 0 0 0 0 0 0;
185820 0 0 0 0 0 0 0 0;
99732 0 0 1 1 0 0 0 0;
198490 0 0 0 0 0 0 0 0;
201250 0 0 1 1 1 1 0 0;
27626 0 0 1 1 0 0 0 0;
69375 0 0 1 1 1 0 0 1];
You can now sort the rows of the matrix however you want. For example, to sort by ID in ascending order, use sortrows:
C = sortrows(C)
To just swap rows around, use a permutation of 1:length(A):
C = C(perm, :)
where perm could be something like [4 5 6 3 2 1 8 7 9 10].
This way, your information is all contained in one structure and the data is always correctly matched to the proper ID.

Matlab - Shuffling matrix values based on some conditions

I have the following two matrices which are outputs of a procedure. The size of the matrices may change but both matrices will always be the same size: size(TwoHopMat_1) == size(Final_matrix)
Example:
TwoHopMat_1 =
0 0 0 0 1
0 0 1 1 0
0 1 0 1 0
0 1 1 0 0
1 0 0 0 0
Final_matrix =
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 0 0 0
1 0 0 0 1
Now I need to shuffle the final_matrix such that i meet the following conditions after shuffling:
Every column should have a minimum of one 1s
If i have a 1 in a particular position of TwoHopMat_1 then that particular position should not have 1 after shuffling.
The conditions should work even if we give matrices of size 100x100.
first step: set one element of each column of the result matrix ,that is not 1 in Final_matrix ,to 1
second step: then remaining ones randomly inserted into positions of the result matrix that are not 1 in Final_matrix and are not 1 in the first step result
TwoHopMat_1=[...
0 0 0 0 1
0 0 1 1 0
0 1 0 1 0
0 1 1 0 0
1 0 0 0 0];
Final_matrix=[...
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 0 0 0
1 0 0 0 1];
[row col] = size(Final_matrix);
result = zeros(row ,col);
%condition 1 & 2 :
notTwoHop = ~TwoHopMat_1;
s= sum(notTwoHop,1);
c= [0 cumsum(s(1:end - 1))];
f= find(notTwoHop);
r = floor(rand(1, col) .* s) + 1;
i = c + r;
result(f(i)) = 1;
%insert remaining ones randomly into the result
f= find(~(result | TwoHopMat_1));
i = randperm(numel(f), sum(Final_matrix(:))-col);
result(f(i)) =1
A possible solution:
function [result_matrix] = shuffle_matrix(TwoHopMat_1, Final_matrix)
% Condition number 2
ones_mat = ones(size(TwoHopMat_1));
temp_mat = abs(TwoHopMat_1 - ones_mat);
% Condition number 1
ones_to_remove = abs(sum(sum(temp_mat)) - sum(sum(Final_matrix)));
while ones_to_remove > 0
% Random matrix entry
i = floor((size(Final_matrix, 1) * rand())) + 1;
j = floor((size(Final_matrix, 2) * rand())) + 1;
if temp_mat(i,j) == 1
temp_mat(i,j) = 0;
ones_to_remove = ones_to_remove - 1;
end
end
result_matrix = temp_mat;
end
Note: this solution uses brute force.

How to sum the 8-neighbor pixel values of the current pixel.

I have a binary image. I want to find the pixel value = 1 and label it as the current pixel. Then, I want to sum its 8-neighbor pixel values. If the summation of the 8-neighbor pixel values of the current pixel = 1, then mark that current pixel with marker. Some part of a binary image as follows:
0 0 0 0 0
0 1 0 0 0
0 0 1 1 0
0 0 0 0 1
0 0 0 0 0
I tried the following matlab code but it has some errors (at this line -> Sums = sum(currentPix, nOffsets);). How can I fix it?
Sums = 0;
S = size(BW,1);
nOffsets = [S, S+1, 1, -S+1, -S, -S-1, -1, S-1]'; %8-neighbors offsets
BW_Out = BW;
for row=1:S
for col=1:S
if BW(row,col),
break;
end
end
idx = sub2ind(size(BW),row,col);
neighbors = bsxfun(#plus, idx, nOffsets);
currentPix = find(BW==1); %if found 1, define it as current pixel
while ~isempty(currentPix)
% new current pixel list is set of neighbors of current list.
currentPix = bsxfun(#plus, currentPix, nOffsets);
currentPix = currentPix(:);
Sums = sum(currentPix, nOffsets); %error at this line
if (Sums==1) %if the sum of 8-neighbor values = 1, mark ROI
plot(currentPix,'r*','LineWidth',1);
end
% Remove from the current pixel list pixels that are already
currentPix(BW_Out(currentPix)) = [];
% Remove duplicates from the list.
currentPix = unique(currentPix);
end
end
I think you can actually do this in one line (after defining a kernel that is)
I = [0 0 0 0 0
0 1 0 0 0
0 0 1 1 0
0 0 0 0 1
0 0 0 0 0];
K = [1 1 1;
1 0 1;
1 1 1;];
(conv2(I,K,'same')==1) & I
ans =
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
Breaking this up:
M = conv2(I,K, 'same'); %// convolving with this specific kernel sums up the 8 neighbours excluding the central element (i.e. the 0 in the middle)
(M==1) & I %// Only take results where there was a 1 in the original image.