duplications for building array of integers or array of chars
Show older comments
Hi guys, I have a problem and it would be pleasure for your assistance.
I have array called y=[5 -1 6 7 9 0 7 3 -4 -4 6];
another array of integers called occurences[1 2 3 4 5 6 7 8 9 10 11]
what I want to implement in matlab is this to loop over array occurences and check at every i if y(i)>0 then I put value 1 in another array/string according to its replications occurences in array occurences at index i, and if y(i) is negative or zero then I put the value 0 according to its occurences in array occurences at index i, for example
y(i=1) >0 because 5>0 then I go to the same index i on the array occurences and check at the same index i=1 how many times should I put "1" in another array of integers or array of chars.
y(i=2) <0 because -1<0 then I go to the same index i on the array occurences and check at the same index i=2 how many times should I put "0" in another array of integers or array of chars.
the output is array of integers or array of chars that must look like [1 0 0 1 1 1 1 1 1 1 ...etc] , the outputs must be values of zero and ones that are accordingly concatenated to number of occurences at index i .
the output is an array could be integers like [1 0 0 1 1 1 1 1 1 1 ...etc] or array of chars like: '1001111111 .....' (doesn't matter which one!)
thanks alot
thanks alot !
1 Comment
madhan ravi
on 15 Jul 2020
Edited: madhan ravi
on 15 Jul 2020
What did you try for your homework? You need to first know the difference between string and character array first.
Answers (1)
madhan ravi
on 15 Jul 2020
y >= 1
14 Comments
Mohamed Jamal
on 15 Jul 2020
madhan ravi
on 15 Jul 2020
Edited: madhan ravi
on 15 Jul 2020
Your question is not clear. Stop adding answers just to make a comment. What would the output be for the below ?
y = [-3, 0 , 0 ,1] % ?
Mohamed Jamal
on 15 Jul 2020
Mohamed Jamal
on 15 Jul 2020
madhan ravi
on 15 Jul 2020
ix = 1:numel(y);
y1 = +(y >= 1);
Output = repelem(y1, ix)
Mohamed Jamal
on 15 Jul 2020
madhan ravi
on 15 Jul 2020
Edited: madhan ravi
on 15 Jul 2020
You’re not good at copying, are you? Read https://www.mathworks.com/matlabcentral/answers/565364-duplications-for-building-array-of-integers-or-array-of-chars#comment_937268 carefully.
Mohamed Jamal
on 15 Jul 2020
Mohamed Jamal
on 15 Jul 2020
Mohamed Jamal
on 15 Jul 2020
Edited: Mohamed Jamal
on 15 Jul 2020
Mohamed Jamal
on 15 Jul 2020
madhan ravi
on 15 Jul 2020
Edited: madhan ravi
on 15 Jul 2020
>> y=[1 2 -1 3]
ix = 1:numel(y);
y1 = +(y >= 1);
Output = repelem(y1, ix)
y =
Columns 1 through 3
1 2 -1
Column 4
3
Output =
Columns 1 through 3
1 1 1
Columns 4 through 6
0 0 0
Columns 7 through 9
1 1 1
Column 10
1
I haven’t seen anyone so stubborn, jeez! You really need to do MATLAB On-ramp course , seem to be really struggling in the basics of MATLAB. By the way you talk as if you are the only one who knows everything , PLEASE STOP IT! Your comments feel like you’re commanding that others should do something that once you state something, that’s not how the real world works my friend! In one of your previous question , you’re in fact arguing about something that you don’t know. You asked the output to be a character array and infact it was a character array but you were arguing about it, I mean you can ask if you don’t know but you speak like as if others are the ones who know nothing.
Mohamed Jamal
on 15 Jul 2020
Mohamed Jamal
on 15 Jul 2020
Categories
Find more on Matrix Indexing 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!