Reorder the numbers after delete some of them in matlab [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a vector [1 2 3 4 ... 100] and another vector storing the numbers I want to delete like [2 3 4]. After deleting, the numbers in original vector should be mapped to another order, for example, 1->1, 5->2, 6->3, etc. Is there any efficient way to do this? Thanks a lot!

I'd use setdiff:
% original vector
A = 1:100;
% elements to be removed
B = [2 3 4 18 21];
% new order (indices)
C = randperm(numel(A)-numel(B));
% Step 1) remove the elements
[D,I] = setdiff(A,B); % ordered
D = A(I); % restore original order
% Step 2) re-order the elements
D = D(C)

You can do:
original_vector = 1:100;
delete_vector = [2 3 4];
for ii = 1:length(delete_vector)
original_vector(original_vector==delete_vector(ii)) = [];
end

Related

How to create a struct in Matlab with parts of anothers structs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have several struct of different dimensions in Matlab. Let us suppose that they were 5, the first is 1x100, the second 1x250, the third 1x200, the fourth 1x100, and the fifth 1x150. I want to assemble a new structure containing only the last element of the each previous structures, that is, I want to get a struct of the 1x5 form. How can I do this?
Create 5 row vectors, then make a row vector from the last elements like this:
>> a=[1:100];
>> b=[1:250];
>> c=[1:200];
>> d=[1:100];
>> e=[1:150];
>> the_lasts = [a(end), b(end), c(end), d(end), e(end)]
sthe_last =
100 250 200 100 150
>>
That also can be generalized into a function, using a file named:
ends_of.m :
function lasts = ends_of (varargin)
% prepare a result row vector with nargin dimention
lasts = zeros(1,nargin);
for i = 1:nargin
element = varargin(i); % get the arg
last = element{}(end); % find the last element
lasts(i) = last; % save it as index i
endfor
endfunction
Then it can be called like this :
>> ends_of ([1,2,3],[4,5,6],[1:44], [3:33], [1,2,3,2,1])
ans =
3 6 44 33 1
>>

Replacing NaN with other value (mean) in MATLAB [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have matrix A such as:
A = [1 2 3 NaN;
1 NaN 2 NaN;
NaN 2 3 4]
I would like to replace every NaN in A with a predefined value such as my columns vector B:
B = [2; 1.5; 3]
So for the second row, I'd like to obtain a new matrix, where every NaN in this particular row is replaced by 1.5. It would be 2 in the first row and 3 in the third row.
Is there a better way to do so other than looping through the entire matrix? I used a for loop with the if and else conditions. However, this solution does not execute very fast.
Here is a solution using repelem (introduced in R2015a). The trick is to repeat the entries of B according to the number of times NaN appears in each row of A.
req = A.';
req(isnan(req)) = repelem(B, sum(isnan(req)));
req = req.';
If you also want to calculate B, use mean with the 'omitnan' flag (introduced in R2015a) i.e.
B = mean(A,2,'omitnan');
You can do it likes the following:
is_NaN = find(isnan(A) > 0); % find index of NaNs
[I,~] = ind2sub(size(A), is_NaN); %transform from linear index to sub index
A(is_NaN) = B(I); % replace all using the row number of each found index
Here you are:
A = [
1 2 3 NaN;
1 NaN 2 NaN;
NaN 2 3 4
];
B = [2; 1.5; 3];
B = repmat(B,1,size(A,2));
idx = isnan(A);
A(idx) = B(idx);
Since you are speaking about a row mean in the title of your question, I suppose that the vector B must not be manually defined, but it should rather be calculated from your A matrix. So:
A = [
1 2 3 NaN;
1 NaN 2 NaN;
NaN 2 3 4
];
B = repmat(nanmean(A,2),1,size(A,2));
idx = isnan(A);
A(idx) = B(idx);

Matlab 3x3 weighted averaging filter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Following advise from another post, I created a basic 3x3 averaging filter as follows:
blurP = zeros(512, 512);
for i = 1:510
for j = 1:510
sum = 0;
for k = i:i+2
for l = j:j+2
sum = sum+P(k,l);
end
end
blurP(i+1,j+1) = mean2(P(i:i+2,j:j+2));
end
end
imshow(P), figure, imshow(blurP, []);
I need to create a weighted filter with two options: One counts the center element (of the 3x3 grid) twice, thus giving me ten elements in total. The other has sixteen elements in total, with the center element counted four times and the adjacent ones counted twice—only the corner elements of the 3x3 grid are counted once.
The easiest option is to just use convolution (via the conv2 function). Then designing your kernels is a simple as writing out exactly what you described:
kernel1 = [1 1 1
1 2 1
1 1 1]/10;
blurP1 = conv2(P, kernel1, 'same');
and
kernel2 = [1 2 1
2 4 2
1 2 1]/16;
blurP2 = conv2(P, kernel2, 'same');

Change of index in for loop Matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a loop like this :
for i = 1:3
M = 1
for M = M:12
while (S(M) == i)
M = M+1
end
end
end
Now for the new incremented value of M in while loop the 'for' loop is not working for this new value.
Any solutions?
The code doesn't make any sense!
You should start to use different names for your parameters,
And note that:
for index = values
program statements
:
end
Avoid assigning a value to the index variable within the body of a loop. The for statement overrides any changes made to the index within the loop.
For the second loop for M = M : 12 is the same as for M = 1 : 12.
MATLAB takes the index values at their first definition, for example,
a = [1 2];
for i = a
disp(i);
a = [1 2 3];
end
You will see that i won't accept the value 3 because at the first use of for, i is set over [1 2].

finding all factors of a number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to Matlab and for the values of a, l and w i need to find all the values for l in the data set and the corresponding w values.
a=10;
l=(0:10)
w=(0:10)
for l,d
if a == l.*w
disp(l)
disp(w)
end
end
Not sure what you want to do, but I think your code could be put as follows:
a = 10;
l = 0:a; %// actually, it would suffice to check numbers up to floor(a/2)
ind = rem(a,l)==0; %// logical index that tells if remainder is zero or not
disp([l(ind); a./l(ind)])
Result:
1 2 5 10
10 5 2 1
You could do it more directly with Matlab's factor function:
f = factor(a);
disp([f; a./f])
Result:
2 5
5 2