Matlab Create an array with element of a for loop - matlab

Here's my code:
N = 1:999;
for i = N
if rem(i,3) == 0 || rem(i,5) == 0
v(i,1) = i
end
end
Te problem is that I get an Array with some zeros in, but I just want an an arraywith the values comforming to my conditions.
How can I fix it?
Thank you!

I think the OP is looking for a result like:
v= N( (rem(N,3)==0) | (rem(N,5)==0) );
though without looping... :-)

I'm assuming that you're using a loop for a reason, and am not removing it from my solution. However, loops should be avoided where possible.
If I understand your question, you're trying to store only those values of i which correspond to a true conditional evaluation. You're problem is that you're using i as your index value inside the assignment statement. Use the end index keyword. Like so:
N = 1:999;
v = [];
for i = N
if rem(i,3) == 0 || rem(i,5) == 0
v(end+1) = i
end
end

Related

How to use for loop to add the previous values in an array in MATLAB?

I have an array like t. It contains the numbers and I would like to add to each number the previous ones. For example: t=[0,2,3,5] and I would like to get tnew=[0,2,5,10]. I tried out this code but it is wrong for sure. (There are 5292 values)
for i=0:5292
t(i)=t(i)+t(i+1)
end
For some array t = [0,2,3,5];, you can just do tnew = cumsum(t).
If you really want to do this in a loop, you need to start from the 2nd index, and keep adding to the value from the previous index
t = [0,2,3,5];
tnew = t;
for ii = 2:numel(t)
tnew(ii) = t(ii) + tnew(ii-1);
end

Return " i " value when if-statement is true

I have created a matrix with row 1 full of strings and 4 other rows with numbers. They are created in a handle class with the object "Projekter".
So in the object "Projekter" row 1, the first value is blank, but the second value is 'Ole'. So I know that 'Ole' is in (1,2). x is the name/string I want to search for, which in this case is 'Ole'.
As you see below it should search row 1 from column 2 untill the last name/string and if i = 'Ole', it should bring me the value 2 because " i " should be equal 2.
A is just a controller if the function works, but at this point it doesn't.
The error it gives is "Undefined function 'eq' for input arguments of type 'cell'."
How do I fix this so it return the " i " value when the statement is correct?
Thank you in advance!
function number(obj,x)
A = [];
for i = 2:size(obj.Projekter,2)
if obj.Projekter(1,i)==x
A = A + 1;
end
end
disp(A)
end
Maybe you have to index the cell content:
your_cell = {'a_string'};
your_string = your_cell{1};
function [returnValue] = number(obj,x)
for i = 2:size(obj.Projekter,2)
if obj.Projekter{1,i}==x
returnValue = i;
return;
end
end
end
Note the change from obj.Projekter(1,i)==x to obj.Projekter{1,i}==x (use curly braces instead of parens). I have then specified that returnValue will hold the value that should be returned by doing function [returnValue] = number(obj,x). We then set returnValue equal to i and return from the function when the condition of the if statement is true.
As suggested in the comments, it is probably better to do:
function [returnValue] = number(obj, x)
returnValue = find(strcmp(x, obj.Projekter) == 1);
strcmp(x, obj.Projektor) will give you an array the length of obj.Projekter with 1's wherever the strings match, and 0's where they don't, you can then find the indices that are set to 1. This has the added benefit of
not using a loop so it's faster
Giving you every occurrence of a match, not just the first one.

Matlab: how to sum fields of two equivalent structures?

I am working in Matlab. I have defined:
a(1).x=1;
a(1).y=2;
a(1).z.w=3;
a(2).x=4;
a(2).y=5;
a(2).z.w=6;
I am now trying to add the fields in the two structures a(1) and a(2) such that I get:
c.x = 5;
c.y = 7;
c.z.w = 9;
Any idea how I can do this in an elegant way? Note that in the original problem the structures have many more fields (around 50).
Thank you very much in advance!
José
Here is a solution, for any depth of struct
The code of the script (or MATLAB command)
a(1).x=1;
a(1).y=2;
a(1).z.w=3;
a(2).x=4;
a(2).y=5;
a(2).z.w=6;
c=a(1);
c = returnStruct(c, a(2));
%{
you can also sum any amount of structs
for i=2:length(a)
c=returnStruct(c, a(i));
end
%}
with the recursive function
function rs = returnStruct(s,a)
fn = fieldnames(s);
for i=1:length(fn)
if isstruct(s.(fn{i}))
s.(fn{i}) = returnStruct(s.(fn{i}), a.(fn{i}));
else
s.(fn{i}) = s.(fn{i})+a.(fn{i});
end
end
rs = s;
end
I tested it for deeper levels of structs and it worked perfectly. Maybe, you have to adapt it slightly for your case, but this should be the way to go.
Unfortunately, any function like struct2cell only converts the first level, so you need something else.
If the deepest substructure level is 2 then this code will work:
fields=fieldnames(a);
for i=1:numel(fields)
if isstruct(a(1).(fields{i}))
fields2=fieldnames(a(1).(fields{i}));
for j=1:numel(fields2)
a(3).(fields{i}).(fields2{j})= a(1).(fields{i}).(fields2{j})+a(2).(fields{i}).(fields2{j});
end
else
a(3).(fields{i})=a(1).(fields{i})+a(2).(fields{i});
end
end
You can have a simple recursive solution
function r = sumfields(s)
if isstruct(s)
for f = fieldnames(s).'
r.(f{1}) = sumfields([s.(f{1})]);
end
else
r = sum(s);
end
end

How do I operate on sevaral arrays in one loop MATLAB

I think it's a simple one
I have 8 arrays T03 and I want to check for some conditions in every one of them in a single loop. Here's what I'm talking about:
while(i<length(RRs)+1)
if T03_i(2,4)>0 && RRs(1:2,i)<0
RRs(1:2,i) = 0;
end
i=i+1;
end
As you see, I want to change elements in RRs array based on conditions in both RRs and T03_1/T03_2/T03_3/.../T03_8. Since T03_i doesn't work, do you have any suggestions?
Seems like the T03_i arrays are of the same size. You can combine them in one 3D array like this:
T3D = cat(3,T03_1,T03_2,T03_3,T03_4,T03_5,T03_6,T03_7,T03_8)
Then
V_mn = squeeze(T3D(m,n,:));
will give you a vector of elements from m-th row and n-th column from all the T03_i arrays at once and you can loop through them like V_mn(i) (here i has the same meaning as in your T03_i).
I suppose that you need all of the T03 arrays to be checked:
while(i<length(RRs)+1)
if T03_1(2,4)>0 ...
&& T03_2(2,4)>0 ...
&& T03_3(2,4)>0 ...
&& T03_4(2,4)>0 ...
&& T03_5(2,4)>0 ...
&& T03_6(2,4)>0 ...
&& T03_7(2,4)>0 ...
&& T03_8(2,4)>0 ...
&& RRs(1:2,i)<0
RRs(1:2,i) = 0;
end
i=i+1;
end

extracting the index of a certain character in a cell array

I have a cell array like this:
and I want to extract the index of 2 in this cell array so I used these lines of codes:
for i = 1:size(idx,1)
if idx{i,1} ~= []
index = i;
end
end
but the code doesn't work.I mean the debuger never enters if beacause it doesn't understand that 2 differs from [].why? and how do you suggest me to write the code?
note that the character will not always be 2 and it may occur in other indexes too.
To test if you variable is empty use ISEMPTY function.
To do it for all elements in a cell array you can use CELLFUN:
index = find(~cellfun(#isempty, idx));
In Matlab, [] means empty, thus:
for i = 1:size(idx,1)
if ~isempty(idx{i,1})
index = i;
end
end