hi all,
I have an array of 365 values and I do some calculations with an index for k=1:365
However, I need to do some different calculations for some k (let's say k=7, k=9,k=11, k=12)
how can I select only these?
I imagine is something like that: for k=1:365
if k=7.....
else...
thanks

 Accepted Answer

It is easiest to address them directly:
v = randn(1,365); % Create Data
k = 1:365; % Index Vector
select_k = [7 9 11 12]; % Select ‘k’ Values
select_v = v(select_k); % Select ‘v’ Values

10 Comments

thanks for the answer, however I have around 107 values for k that i need to do something and 258 to do something else. So how can I choose the 258 values? (I mean not manually)
The easiest way is to use ‘logical vectors’.
Expanding on my code, I created ‘Lk’ (logical vector of selected values to do ‘something’ with), and ‘Lnk’ (logical vector of ‘not k’) to do ‘something else’ with. There is one logical index in each vector for all 365 elements of your original vector.
The Code
v = randn(1,365); % Create Data
k = 1:365; % Index Vector
select_k = [7 9 11 12]; % Select ‘k’ Values
select_v = v(select_k); % Select ‘v’ Values
Lv = logical(ones(size(k))); % Logical Vector For All 365 Indices
Lkk(select_k) = Lv(select_k > 0); % Create Limited Logical Vector Of Selected Values
Lk = Lv & [Lkk ~Lv(length(Lkk)+1:end)]; % Full Logical Vector Of Selected Values
Lnk = ~Lk; % Full Logical Vector Of Non-Selected Values
thanks a lot!
My pleasure!
This is an interesting problem!
Nikolas Spiliopoulos
Nikolas Spiliopoulos on 25 Feb 2017
Edited: Nikolas Spiliopoulos on 25 Feb 2017
I have another question, cos I think I didn't explain something well!
-I have 2 arrays (so probably I have to do the same for both of them)
-The values that I am using are not a particular value but a column (from each matrix)
so that's why i think I get an error about the dimensions
any ideas how to fix it?
thanks again
You implied a row vector in your Question. I do not know what error you are getting, or the sizes of your arrays. You must index the arrays correctly in order to use them with my logical vectors.
This works:
k = 1:365; % Index Vector
select_k = [7 9 11 12]; % Select ‘k’ Values
select_v = v(select_k); % Select ‘v’ Values
Lv = logical(ones(size(k))); % Logical Vector For All 365 Indices
Lkk(select_k) = Lv(select_k > 0); % Create Limited Logical Vector Of Selected Values
Lk = Lv & [Lkk ~Lv(length(Lkk)+1:end)]; % Full Logical Vector Of Selected Values
Lnk = ~Lk; % Full Logical Vector Of Non-Selected Values
A = randi(99, 365, 2); % Create Data Array (365 rows, 2 columns)
RowsToDoSomething = A(Lk,:);
RoswToDoSomethingElse = A(Lnk,:);
I chose ‘A’ to be (365x2) to illustrate the correct approach. The new variables are self-documenting.
This will work for any (365xN) matrix.
See the documentation on Matrix Indexing to understand the indexing into ‘A’ I use here.
thanks again for the answer, I am gonna see how it goes asap! cheers!
In your newest Question, adding a value until a particular value, you wrote:
‘I have an array 17x365 and some initial values in the first row.’
That is 17 rows and 365 columns.
This is not what you led me to believe, and information you did not provide anywhere in your Question or Comments here.
The correct addressing for your (17x365) matrix will be:
A = randi(99, 17, 365); % Create Data Array (17 rows, 365 columns)
RowsToDoSomething = A(:,Lk);
RoswToDoSomethingElse = A(:,Lnk);
If you want our help to write code that works for you, you have to provide us with the complete and correct information necessary to Answer your Question!
Please re-Accept my Answer. This will work with your matrix.
the new question is a different one,
thanks a lot
you are very helpful!
sorry for being a pain..I just started using matlab!
I am learning a lot from you
thanks!
My pleasure.
You are not a pain. If you have previous programming experience, learning MATLAB will be relatively easy, once you get used to the conventions and programming environment. If you have no programming experience, learning any programming language for the first time will be a challenge.
Learning MATLAB will be much easier if you use the online tutorials. Begin with Getting Started.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!