How can I identify value of an array?
Show older comments
Hi! I'm creating a code with Matlab but I've a problem.
I have an array, that is:
C = [2,1,0];
I've identified position of values, like:
C1 = C (1)
C2 = C (2)
C3 = C (3)
but I'd like to insert an input, that asks to user to insert the different values of array, so I can not know how many values will be in array.
In this case, how can I identify the position of single elements, given an input with a generic number of elements?
I thought to create a 'for cycle' but I have many problems!
Thank you
7 Comments
Dennis
on 30 Apr 2018
you can use size(C,2) to check how many entries each row of your matrix has.
If you want to find the index of a certain value you can use find.
I want to suggest that you do not name your variables C1, C2, C3 and instead use their indexes: C(1), C(2), C(3).
Guillaume
on 30 Apr 2018
Simple rule: If you're numbering variables, you're doing it wrong. Follow Dennis' link to learn more.
I'm really not clear what your problem is. Maybe explain what you want to do afterward. Why would you even need C1, C2, etc.?
sc
on 30 Apr 2018
Siyu Guo
on 30 Apr 2018
I have no idea on what you are wanting to do. Could you plz give a more detailed example and show your operation flow?
Guillaume
on 30 Apr 2018
given an input with values in array, identifies position of all values in array
What does that actually mean? What is a position? What are you going to do with these positions?
You're not telling us the big picture. That would really help because at the moment your question is very strange.
sc
on 30 Apr 2018
@sc: why do you need to do this? Using indexing is simple and very efficient. What you are trying to do is slow, complex, buggy, and hard to debug. Experienced MATLAB users use indexing: you can too.
EDIT: see also later question, where the OP asks about what they are actually trying to achieve:
Accepted Answer
More Answers (1)
Please, do read the discussion on the link in Dennis' comment. The code you've written is exactly why we say not to number variables and use eval. It's pointless complicated.
Before that,
b = 9;
num_int = numel(files)/b;
for k = 1 : num_int
Ck = [];
for n = 1:b
C_end = C(:,:,K+num_int*(n-1));
Ck = cat(3,Ck,C_end);
end
I'm not entirely sure what you're doing here since K is not defined. Assuming that there is a typo and K is k then all you're doing is deinterlacing data. In which case:
b = 9;
assert(mod(numel(files), b) == 0, 'Number of files must be a multiple of b'); %always a good idea to check your assumptions
num_int = numel(files) / b;
Ck = C(:, :, (1:num_int) + (0:b-1)' * num_int); %requires R2016b
%Ck = C(:, :, bsxfun(@plus, 1:num_int, (0:b-1)' * num_int)); %on earlier versions
This will be much faster than your double loop and your growing Ck array.
Then the whole numbered Cbxx, CBxx_new, etc. is completely pointless. If all you want to do is reorder things, then use indexing:
C_input = [1000,700,500,350,200,100,50,15,0];
order = [9 1 2 3 4 5 6 7 8 9];
new_cb = C_input(order);
C = Ck(:, :, order);
or something like that. Again, I'm really not clear on what you're trying to do. One thing for sure, you do not need numbered variables and eval.
3 Comments
sc
on 2 May 2018
Guillaume
on 2 May 2018
I'm afraid I don't really understand what exactly changes with user input and what you want to do with it. An array does not contain variables. Can you describe a bit better what's supposed to change and what calculation should be done as a result.
One thing for sure, even if the size of an array is not fixed, indexing is going to be the solution.
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!