C or Matlab row, column, array problem question [closed] - arrays

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 2 months ago.
Improve this question
enter image description here
k=15;
for i=1:k:length(aa) % aa = 100
for j=1:k:length(bb) % bb = 200
result(j,i) = function(a, b, c, d)
end
end
I am trying to save the calculated value by extracting k by 15 steps.
If you do it this way, the next value will come out from the 1st row, 1st column, 16th row, 1st column, as shown in the picture above, and the value of 0 will come out in between.
So, I'm trying to write additional code that pulls them out in sequence like the right part of the picture...
for ii = size_aa:-1:1
if result(ii,:) == 0
result(ii,:) = [];
end
end
I tried the method of removing zero elements, but it takes too much time. Is there any other way?

Once you have your result matrix, you can use result(result == 0) = []; to remove zero elements and end up with a vector of non-zero results. Here's an example derived from the code the OP posted (I replaced function(a,b,c,d) with rand() for the sake of the example):
k=15;
for i=1:k:100
for j=1:k:200
result(j,i) = rand();
end
end
result(result == 0) = [];
Alternatively, the solution proposed by Cris Luengo generates a matrix that doesn't contain zero elements to start with; I plugged his idea in my sample code:
k=15;
for i=1:k:100
for j=1:k:200
result((j-1)/k+1, (i-1)/k+1) = rand();
end
end

Related

MATLAB Basics - interpreting parentheses and colons in array manipulation [closed]

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 3 years ago.
Improve this question
What does it mean in MATLAB when I do:
array = array (1:number)
and what does it mean:
array = array(indexes_array)
and finally, what does it mean:
array = array(indexes_array,:)
Answers, according to MATLAB docs:
1)
array = array (1:number)
The colon notation in MATLAB provides an easy way to extract a range of elements from v:
v(3:7) % Extract the third through the seventh elements
ans =
9 4 2 11 7
2)
array = array(indexes_array)
array is reorganized according to indexes_array order, assuming that indexes_array is composed of indexes.
3)
array = array(indexes_array,:)
just the same as number 1), array is reorganized according to all the rows of indexes_array, ignoring the columns.
References:
https://www.mathworks.com/help/matlab/math/array-indexing.html
https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html

How do I write a function for identifying odd positions in matrix [closed]

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 4 years ago.
Improve this question
How to create a function that generates all the elements in odd positions (e.g. 1,1 1,3 ) in a say...5-by-8 matrix?
I assume that by "odd" you mean where both the index for row and column are odd. This means your resulting matrix should be 3 by 4. Anyway, your code is below.
function Anew = yourFunc(A)
%Create a test matrix of dimensions 5x8 of random numbers from 1-10
A = randi(10,5,8);
%Set the values of all elements with either index being an even number equal to zero
for r = 1 : size(A,1)
for c = 1 : size(A,2)
if rem(r,2) == 0 || rem(c,2) == 0
A(r,c) = 0;
else
continue
end
end
end
%Delete rows that contain ALL zeros
for r = 1 : (size(A,1)/2) + 1
if A(r,:) == 0
A(r,:) = [];
end
end
%Delete columns that contain ALL zeros
for c = 1 : (size(A,2)/2) + 1
if A(:,c) == 0
A(:,c) = [];
end
end
%State final answer
Anew = A;
end
Please note that there may be a more efficient code solution, but I have never studied computer science (not my engineering discipline), so I don't know any fancy stuff.
Indexing will stop at the greatest possible value. So indexing x=1:2:4 will generate x = [1 3]. x=1:2:1 will generate x = 1. So now you just need to figure out how many elements are in each matrix row and column.
Pro-Tip: everywhere possible in any code you write, use the length() function for indexing. With the code below, A can be any size matrix. This prevents needing to change your code when you change the matrix you want to analyze.
for row=1:2:length(A(:,1))
for col=1:2:length(A(1,:))
% do some operation on A(row,col)
end
end

Print random element from array with Lua [closed]

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 7 years ago.
Improve this question
What i want to do,is print random elements from array,heres example code:
myTable = { "one", "two", "three","four"}
-- print here: one,three,four
Printing random element is simple -- print(myTable[math.random(#myTable)]) -- but if you need to make each printed element unique, then you better shuffle the elements in the array and print the resulting elements one-by-one. You may check this SO answer for ideas.
If you want N amount of elements, you need to make use of a loop:
local myTable = { "one", "two", "three","four"}
local result = {}
for i=1,3 do -- N here, e.g 3 if you want 3 elements
result[i] = table.remove(myTable,math.random(#myTable))
end
print(table.concat(result,", "))
-- "four, two, three" as an example
The code will error if you request more elements than there are in the table. If you want to reuse the table later on, you'll have to copy it, as this code actually removes elements from the table.

how to Split a list/array based on index of array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a list/array of reservation Items
Dim reservationItemsarr = ObjTIReadRS.TravelItinerary.ItineraryInfo.ReservationItems.SelectMany(Function(x) x.FlightSegment).ToArray()
now based on some conditions i have made another array which has index of values.
i need to split the
reservationItemsarr
i need to create a new list/array that will hold the values based on my index array.
so final result should be be like
list(0) = reservationItemsarr(0) and reservationItemsarr(1)
list(1) = reservationItemsarr(2) and reservationItemsarr(3) and
reservationItemsarr(4)
list(2) = reservationItemsarr(5) and reservationItemsarr(6) and
reservationItemsarr(7)
my problem is not creating a collection of collection, but i need a way to split like above.
A basic algorithm to get you started:
Dim results As New List(Of IEnumerable(Of ReservationItem))()
Dim remainder As IEnumerable(Of ReservationItem) = reservationItemsArr
Dim lastIndex As Integer = 0
For i As Integer = 0 To index.Length - 1
results.Add(remainder.Take(index(i) - lastIndex))
remainder = remainder.Skip(index(i) - lastIndex)
lastIndex = index(i)
Next
results.Add(remainder)
You need to take care of edge cases such as index pointing at the last element or outside the array.

how to convert from cells array in matlab [closed]

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 8 years ago.
Improve this question
I have 2 cells functions (resulted from certain code) as follows:
p =
'GO:0008150'
'GO:0016740'
'GO:0016787'
'GO:0008150'
'GO:0016740'
'GO:0016740'
'GO:0016787'
'GO:0016787'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0004672'
'GO:0008150'
'GO:0008150'
'GO:0006810'
'GO:0016192'
'GO:0006810'
'GO:0005215'
c =
'GO:0016740'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0004672'
'GO:0016779'
'GO:0004386'
'GO:0003774'
'GO:0016298'
'GO:0016192'
'GO:0005215'
'GO:0030533'
'GO:0016787'
'GO:0006810'
'GO:0006412'
'GO:0003774'
'GO:0005215'
'GO:0030533'
I have a code that it works fine with single values (numericals or characters), but it doesn't work with the above cellsarrays, and the following error message appears:
??? Undefined function or method 'eq' for input arguments of type 'cell'.
If there any idea about how to convert from cells function to any kind which can accept the following code:
level1_root=setdiff(p,c) % to find the cells from p not in c
for i=1:length(p)
a=[p(i),c(i)];
if a(1,:)==level1_root
level=a(2);
level=[level a(2)]
else
end
end
In your code, a is a cell array of two strings, while level1_root is a cell array with one string. You can not compare cell arrays of strings using ==. You should rather to a strcmp. For example, something like this:
level1_root=setdiff(p,c) % to find the cells from p not in c
for i=1:length(p)
a=[p(i),c(i)];
if strcmp(a{1}, level1_root{:})
level=a(2);
level=[level a(2)]
else
end
end
However, I am not sure if the above code will work for you as it is. It assumes that level1_root has 1 element only. That is the case in the example arrays, but may not be in general. You will have to change the code to suit your needs.

Resources