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].
Related
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
>>
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');
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
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 an array with variables like S, SV, V, etc. I want to create a new array only with the V variable, so, for example:
V
SV
S
V
becomes
V
V
After this, I need to know the indices where each one of the V variables was found, in this case indices 1 and 4.
P.S I already tried using an if in a for loop like this:
for i=1:744;
if most_common_string{i} == 'V'
periodo=most_common_string{i};
end
end
but I'm not getting an array with all the V's. In fact I'm only getting a cell.
I also tried the find function to get the indices, but it is not working.
You don't need the second array for that; you can use cellfun. Also, to compare strings use strcmp, not == (that's probably why your code is not working):
>> array = {'V';'SV';'S';'V'};
>> ind = cellfun(#(s) strcmp(s,'V'), array)
ind =
1
0
0
1
>> find(ind)
ans =
1
4
One way to do this is to fix your for loop. The problem is, you're always assigning the value to the same variable. Not much to gain there. You need to add to an array:
result = {};
indices = [];
for i=1:numel(most_common_string)
if most_common_string{i} == 'V'
indices = [indices;i];
result = [result;most_common_string(i)];
end
end
Note that Code Analyzer might complain about your array changing sizes during loop iterations. You can't really avoid that since you don't kmow what size the array will have.
find probably doesn't work as you expect it to because it's not fit for use with cell arrays.
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