Repeat element in an array and add behind it
this is a loop in my script:
for k=1:K
block= squeeze(Data_orig(k,:,:)); % K x M x N
a=2;
for dd=1:d
for w=(a*dd(a1)):(a*dd)
% `mtxr` is P x M x N/a.
data(:,w)=mtxr(:,:,dd)*block(:,w); % M x N
end
end
mtxr_tr(:,:,k)=data.'; % (N x M x K)
end
Let me explain this loop. [example] data(:,w)=mtxr(:,:,dd)*block(:,w);
mtxr= [ a1, a2, a3], where a1, a2, a3 are arrays
block= [ b1, b2, b3, b4, b5, b6], where b1, ...b5 are arrays
Result
data
:
1) a1 * b1
2) a1 * b2
3) a2 * b3
4) a2 * b4
5) a3 * b5
6) `a3 * b6'
I would like to create an array B
as a1 a1 a2 a2 a3 a3
. B
is constructed from mtxr
, but every element is copied and added behind.
How can I create the array B
?
See also questions close to this topic

Format output of an array map
I am trying to make a function that counts duplicates, this works but not in the output format I need. This is my function:
var duplicateCount = {}; countryArray.forEach(e => duplicateCount[e] = duplicateCount[e] ? duplicateCount[e] + 1 : 1); result5 = Object.keys(duplicateCount).map(e => {return {key:e, count:duplicateCount[e]}}); console.log("result5", result5);
The output I get is:
result5 (3) [{…}, {…}, {…}] 0: {e: "CRM", count: 6} 1: {e: "TSA", count: 8} 2: {e: "PCS", count: 3} length: 3 __proto__: Array(0)
The output I need is:
result5 (3) [{…}, {…}, {…}] 0: {"CRM", 6} 1: {"TSA", 8} 2: {"PCS", 3} length: 3 __proto__: Array(0)
Any help is good. Thank!!

Trying to remove duplicate elements with the same values that are next to each other in an array (they have to preserve the original order)
So). This removes all duplicates. But I cann't figure out the logic for removing duplicates that are next to each other only.
For example: input:('FFNNbbffnnNN'); output:[F, N, b, f, n, N];
var uniqueInOrder = function(iterable){ var newArr =[]; var len = iterable.length; for(var i = 0; i < len ; i ++){ if( newArr.indexOf(iterable[i]) === 1){ newArr.push(iterable[i]) } } return newArr; } uniqueInOrder('ffssSnnsS');
Here I tried a little bit.. meh.. begging for tips. Thank you!
var uniqueInOrder = function(iterable){ var newArr =[]; var len = iterable.length; var first = iterable[0]; for(var i = 0; i < len ; i ++){ if( newArr.indexOf(first) !== newArr.indexOf(first + 1){ newArr.push(iterable[i]) } } return newArr; } uniqueInOrder('ffssSnnsS');

Add to favourites and view from favourites with React Hooks?
I have a state
const [ideas, setIdeas] = useState([{title:"test", favourite:false]);
Component
Idea.jsx
returnsprops.title
and a button "fav".App.jsx
maps through theidea[]
and renders eachidea.title
in<Item title = {idea.title}/>
on the page.
Problem: Every time when "fav" is clicked I want to toggle
ideas[index].favourite
. How to change a value offavourite
only for anidea
that was clicked?How to add this exact
idea
to the arrayfavourites[]
?App.jsx
function App() { const [ideas, setIdeas] = useState([{title:"test", favourite:false}]); const [isClicked, setIsClicked] = useState(false) function showAllIdeas () { setIsClicked(prevValue => { return !prevValue } ) } function mapIdeas(){return ideas.map((ideaItem, index) => { return (<Idea key = {index} id = {index} title = {ideaItem.title} /> ); })} return ( <div> <Fab color="primary" onClick={showAllIdeas}>{expandText()}</Fab> {isClicked && mapIdeas()} </div>) }
Item.jsx
function Idea(props) { const [isClicked, setIsClicked] = useState(false) function handleClick(){ setIsClicked(prevValue => { return !prevValue }) } console.log(isClicked) return( <div className={"idealist" } ><p>{props.title} {isClicked ? <StarIcon onClick={handleClick}/> :<StarBorderIcon onClick=. {handleClick}/>}</p> </div> ) }

Convert polyshape to set of coordinates array
I wish to find the shortest distance between a point and a
polyshape
using thedistancepointpolygon
function from Matlabgeom2d
library. Thedistancepointpolygon
function does it for polygon and therefore how can I convert anypolyshape
to a Nby2 array containing vertex coordinates? I did it manually and I am looking for a general solution.polySquare = polyshape([0 5 5 0], [10 10 15 15]); plot(polySquare) square = [0 10; 5 10; 5 15; 0 15]; p0 = [5 10]; distancePointPolygon(p0, square)

Reading the last 2 lines of a .log file via Matlab
The problem that I am attempting to solve is as follows:
I have a .log file that is updated every x seconds (an interval that I can change), with updated status information from a piece of test equipment. At each interval, another line is added to the .log file, with the updated information. My goal is to have the most recent status information (the last two lines of the .log file) easily viewable in Matlab.
Here is an example of what each update looks like, in case that is relevant (a single line of text):
What I have tried:
I used the readtable command (shown below) to view the information in the .log file, but that gives me the entire .log file every time the function is called, when I only want/need the last two lines.
data = readtable('FileName.log','FileType','text')
I know that this would be simpler if I was working with a .csv or .xlsx file, but the test equipment only updates the .log file, so I cannot just change the file type, as it would not get updates then.
Any advise would be appreciated.

Generating unitriangular matrices in OctaveMATLAB
I have a very simple question, I am struggling to find an answer to. I would like to craft (upper or lower) unitriangular and unitrapezoidal matrices in Octave. Is there an efficient and elegant way to achieve that?
Let,
A = [1 2 3; 4 5 6; 7 8 9]
. I can obtain upper or lower triangular matrixB
withB = triu(A)
andB = tril(A)
. Consider a lower triangular caseB = tril(A)
. To extract the elements below the main diagonal I useB = tril(A,1)
. That will result in0 0 0 4 0 0 7 8 0
But how to obtain ones on the main diagonal of
B
now? One workaround that I have is generating an identity matrix and adding it toB
:B = tril(A,1) + eye(size(A)) 1 0 0 4 1 0 7 8 1
But I feel this is suboptimal due to the addition. Is there a better way?